blob: 854c09d01c09db649a2d6739e5ec99c34793e335 [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 Moolenaar4913d422022-10-17 13:13:32 +010096 if ((typep->type_curr->tt_type == VAR_ANY
97 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaar078a4612022-01-04 15:17:03 +000098 && idxtype == &t_string)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000099 vartype = VAR_DICT;
100 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
101 {
102 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
103 return FAIL;
104 if (is_slice)
105 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000106 idxtype = get_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000107 if (need_type(idxtype, &t_number, -2, 0, cctx,
108 FALSE, FALSE) == FAIL)
109 return FAIL;
110 }
111 }
112
113 if (vartype == VAR_DICT)
114 {
115 if (is_slice)
116 {
117 emsg(_(e_cannot_slice_dictionary));
118 return FAIL;
119 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000120 if (typep->type_curr->tt_type == VAR_DICT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000121 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000122 typep->type_curr = typep->type_curr->tt_member;
123 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000124 // empty dict was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000125 typep->type_curr = &t_any;
126 if (typep->type_decl->tt_type == VAR_DICT)
127 {
128 typep->type_decl = typep->type_decl->tt_member;
129 if (typep->type_decl == &t_unknown)
130 // empty dict was used
131 typep->type_decl = &t_any;
132 }
133 else
134 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000135 }
136 else
137 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000138 if (need_type(typep->type_curr, &t_dict_any, -2, 0, cctx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000139 FALSE, FALSE) == FAIL)
140 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000141 typep->type_curr = &t_any;
142 typep->type_decl = &t_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000143 }
Bram Moolenaard0132f42022-05-12 11:05:40 +0100144 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL
145 || generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000146 return FAIL;
147 if (keeping_dict != NULL)
148 *keeping_dict = TRUE;
149 }
150 else if (vartype == VAR_STRING)
151 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000152 typep->type_curr = &t_string;
153 typep->type_decl = &t_string;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000154 if ((is_slice
155 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
156 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
157 return FAIL;
158 }
159 else if (vartype == VAR_BLOB)
160 {
161 if (is_slice)
162 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000163 typep->type_curr = &t_blob;
164 typep->type_decl = &t_blob;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000165 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
166 return FAIL;
167 }
168 else
169 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000170 typep->type_curr = &t_number;
171 typep->type_decl = &t_number;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000172 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
173 return FAIL;
174 }
175 }
Bram Moolenaar4913d422022-10-17 13:13:32 +0100176 else if (vartype == VAR_LIST || typep->type_curr->tt_type == VAR_ANY
177 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000178 {
179 if (is_slice)
180 {
181 if (generate_instr_drop(cctx,
182 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
183 2) == FAIL)
184 return FAIL;
Bram Moolenaar5f4ef5f2022-02-06 18:36:53 +0000185 // a copy is made so the member type is no longer declared
186 if (typep->type_decl->tt_type == VAR_LIST)
187 typep->type_decl = &t_list_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000188 }
189 else
190 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000191 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000192 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000193 typep->type_curr = typep->type_curr->tt_member;
194 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000195 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000196 typep->type_curr = &t_any;
197 if (typep->type_decl->tt_type == VAR_LIST)
198 {
199 typep->type_decl = typep->type_decl->tt_member;
200 if (typep->type_decl == &t_unknown)
201 // empty list was used
202 typep->type_decl = &t_any;
203 }
204 else
205 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000206 }
207 if (generate_instr_drop(cctx,
208 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
209 == FAIL)
210 return FAIL;
211 }
212 }
213 else
214 {
215 switch (vartype)
216 {
217 case VAR_FUNC:
218 case VAR_PARTIAL:
219 emsg(_(e_cannot_index_a_funcref));
220 break;
221 case VAR_BOOL:
222 case VAR_SPECIAL:
223 case VAR_JOB:
224 case VAR_CHANNEL:
225 case VAR_INSTR:
226 case VAR_UNKNOWN:
227 case VAR_ANY:
228 case VAR_VOID:
229 emsg(_(e_cannot_index_special_variable));
230 break;
231 default:
232 emsg(_(e_string_list_dict_or_blob_required));
233 }
234 return FAIL;
235 }
236 return OK;
237}
238
239/*
240 * Generate an instruction to load script-local variable "name", without the
241 * leading "s:".
242 * Also finds imported variables.
243 */
244 int
245compile_load_scriptvar(
246 cctx_T *cctx,
247 char_u *name, // variable NUL terminated
248 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100249 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000250{
251 scriptitem_T *si;
252 int idx;
253 imported_T *import;
254
255 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
256 return FAIL;
257 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000258 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000259 if (idx >= 0)
260 {
261 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
262
263 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
264 current_sctx.sc_sid, idx, sv->sv_type);
265 return OK;
266 }
267
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000268 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000269 if (import != NULL)
270 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000271 char_u *p = skipwhite(*end);
272 char_u *exp_name;
273 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100274 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000275 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000276 int done = FALSE;
277 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000278
279 // Need to lookup the member.
280 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000281 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000282 semsg(_(e_expected_dot_after_name_str), start);
283 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000284 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000285 ++p;
286 if (VIM_ISWHITE(*p))
287 {
288 emsg(_(e_no_white_space_allowed_after_dot));
289 return FAIL;
290 }
291
292 // isolate one name
293 exp_name = p;
294 while (eval_isnamec(*p))
295 ++p;
296 cc = *p;
297 *p = NUL;
298
Bram Moolenaard041f422022-01-12 19:54:00 +0000299 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100300 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
301 // "import autoload './dir/script.vim'" or
302 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100303 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100304
305 if (res == OK)
306 {
307 if (si->sn_autoload_prefix != NULL
308 && si->sn_state == SN_STATE_NOT_LOADED)
309 {
310 char_u *auto_name =
311 concat_str(si->sn_autoload_prefix, exp_name);
312
313 // autoload script must be loaded later, access by the autoload
314 // name. If a '(' follows it must be a function. Otherwise we
315 // don't know, it can be "script.Func".
316 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100317 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100318 else
319 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
320 vim_free(auto_name);
321 done = TRUE;
322 }
323 else if (si->sn_import_autoload
324 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100325 {
326 // If a '(' follows it must be a function. Otherwise we don't
327 // know, it can be "script.Func".
328 if (cc == '(' || paren_follows_after_expr)
329 {
330 char_u sid_name[MAX_FUNC_NAME_LEN];
331
332 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100333 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100334 }
335 else
336 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
337 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100338 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100339 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100340 else
341 {
342 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
343 cctx, NULL, TRUE);
344 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100345 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100346
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000347 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000348 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000349 if (done)
350 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000351
352 if (idx < 0)
353 {
354 if (ufunc != NULL)
355 {
356 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100357 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000358 return OK;
359 }
360 return FAIL;
361 }
362
363 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
364 import->imp_sid,
365 idx,
366 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000367 return OK;
368 }
369
Bram Moolenaard0132f42022-05-12 11:05:40 +0100370 // Can only get here if we know "name" is a script variable and not in a
371 // Vim9 script (variable is not in sn_var_vals): old style script.
372 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000373 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000374}
375
376 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000377generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000378{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000379 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000380 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000381
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000382 // Reject a global non-autoload function found without the "g:" prefix.
383 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000384 return FAIL;
385
386 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000387 compile_type = get_compile_type(ufunc);
388 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000389 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000390 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100391 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000392}
393
394/*
395 * Compile a variable name into a load instruction.
396 * "end" points to just after the name.
397 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
398 * When "error" is FALSE do not give an error when not found.
399 */
400 int
401compile_load(
402 char_u **arg,
403 char_u *end_arg,
404 cctx_T *cctx,
405 int is_expr,
406 int error)
407{
408 type_T *type;
409 char_u *name = NULL;
410 char_u *end = end_arg;
411 int res = FAIL;
412 int prev_called_emsg = called_emsg;
413
414 if (*(*arg + 1) == ':')
415 {
416 if (end <= *arg + 2)
417 {
418 isntype_T isn_type;
419
420 // load dictionary of namespace
421 switch (**arg)
422 {
423 case 'g': isn_type = ISN_LOADGDICT; break;
424 case 'w': isn_type = ISN_LOADWDICT; break;
425 case 't': isn_type = ISN_LOADTDICT; break;
426 case 'b': isn_type = ISN_LOADBDICT; break;
427 default:
428 semsg(_(e_namespace_not_supported_str), *arg);
429 goto theend;
430 }
431 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
432 goto theend;
433 res = OK;
434 }
435 else
436 {
437 isntype_T isn_type = ISN_DROP;
438
439 // load namespaced variable
440 name = vim_strnsave(*arg + 2, end - (*arg + 2));
441 if (name == NULL)
442 return FAIL;
443
444 switch (**arg)
445 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100446 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000447 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000448 case 's': if (current_script_is_vim9())
449 {
450 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
451 *arg);
452 vim_free(name);
453 return FAIL;
454 }
Kota Kato948a3892022-08-16 16:09:59 +0100455 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000456 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000457 else
458 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100459 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000460 break;
461 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
462 {
463 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000464 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000465 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000466 else
467 isn_type = ISN_LOADG;
468 }
469 else
470 {
471 isn_type = ISN_LOADAUTO;
472 vim_free(name);
473 name = vim_strnsave(*arg, end - *arg);
474 if (name == NULL)
475 return FAIL;
476 }
477 break;
478 case 'w': isn_type = ISN_LOADW; break;
479 case 't': isn_type = ISN_LOADT; break;
480 case 'b': isn_type = ISN_LOADB; break;
481 default: // cannot happen, just in case
482 semsg(_(e_namespace_not_supported_str), *arg);
483 goto theend;
484 }
485 if (isn_type != ISN_DROP)
486 {
487 // Global, Buffer-local, Window-local and Tabpage-local
488 // variables can be defined later, thus we don't check if it
489 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000490 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491 }
492 }
493 }
494 else
495 {
496 size_t len = end - *arg;
497 int idx;
498 int gen_load = FALSE;
499 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100500 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100501 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000502
503 name = vim_strnsave(*arg, end - *arg);
504 if (name == NULL)
505 return FAIL;
506
507 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
508 {
509 script_autoload(name, FALSE);
510 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
511 }
512 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
513 == OK)
514 {
515 if (gen_load_outer == 0)
516 gen_load = TRUE;
517 }
518 else
519 {
520 lvar_T lvar;
521
522 if (lookup_local(*arg, len, &lvar, cctx) == OK)
523 {
524 type = lvar.lv_type;
525 idx = lvar.lv_idx;
526 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100527 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000528 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100529 outer_loop_depth = lvar.lv_loop_depth;
530 outer_loop_idx = lvar.lv_loop_idx;
531 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000532 else
533 gen_load = TRUE;
534 }
535 else
536 {
537 // "var" can be script-local even without using "s:" if it
538 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000539 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000540 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100541 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000542
543 // When evaluating an expression and the name starts with an
544 // uppercase letter it can be a user defined function.
545 // generate_funcref() will fail if the function can't be found.
546 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000547 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000548 }
549 }
550 if (gen_load)
551 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
552 if (gen_load_outer > 0)
553 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100554 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
555 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000556 cctx->ctx_outer_used = TRUE;
557 }
558 }
559
560 *arg = end;
561
562theend:
563 if (res == FAIL && error && called_emsg == prev_called_emsg)
564 semsg(_(e_variable_not_found_str), name);
565 vim_free(name);
566 return res;
567}
568
569/*
570 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100571 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000572 * Returns FAIL if compilation fails.
573 */
574 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100575compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000576{
LemonBoyf3b48952022-05-05 13:53:03 +0100577 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000578 garray_T save_ga = cctx->ctx_instr;
579 int expr_res;
580 int trailing_error;
581 int instr_count;
582 isn_T *instr = NULL;
583
584 // Remove the string type from the stack.
585 --cctx->ctx_type_stack.ga_len;
586
587 // Temporarily reset the list of instructions so that the jump labels are
588 // correct.
589 cctx->ctx_instr.ga_len = 0;
590 cctx->ctx_instr.ga_maxlen = 0;
591 cctx->ctx_instr.ga_data = NULL;
592 expr_res = compile_expr0(&s, cctx);
593 s = skipwhite(s);
594 trailing_error = *s != NUL;
595
596 if (expr_res == FAIL || trailing_error
597 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
598 {
599 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000600 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000601 clear_instr_ga(&cctx->ctx_instr);
602 cctx->ctx_instr = save_ga;
603 ++cctx->ctx_type_stack.ga_len;
604 return FAIL;
605 }
606
607 // Move the generated instructions into the ISN_INSTR instruction, then
608 // restore the list of instructions.
609 instr_count = cctx->ctx_instr.ga_len;
610 instr = cctx->ctx_instr.ga_data;
611 instr[instr_count].isn_type = ISN_FINISH;
612
613 cctx->ctx_instr = save_ga;
614 vim_free(isn->isn_arg.string);
615 isn->isn_type = ISN_INSTR;
616 isn->isn_arg.instr = instr;
617 return OK;
618}
619
620/*
621 * Compile the argument expressions.
622 * "arg" points to just after the "(" and is advanced to after the ")"
623 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100624 int
LemonBoyf3b48952022-05-05 13:53:03 +0100625compile_arguments(
626 char_u **arg,
627 cctx_T *cctx,
628 int *argcount,
629 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000630{
631 char_u *p = *arg;
632 char_u *whitep = *arg;
633 int must_end = FALSE;
634 int instr_count;
635
636 for (;;)
637 {
638 if (may_get_next_line(whitep, &p, cctx) == FAIL)
639 goto failret;
640 if (*p == ')')
641 {
642 *arg = p + 1;
643 return OK;
644 }
645 if (must_end)
646 {
647 semsg(_(e_missing_comma_before_argument_str), p);
648 return FAIL;
649 }
650
651 instr_count = cctx->ctx_instr.ga_len;
652 if (compile_expr0(&p, cctx) == FAIL)
653 return FAIL;
654 ++*argcount;
655
LemonBoyf3b48952022-05-05 13:53:03 +0100656 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000657 && cctx->ctx_instr.ga_len == instr_count + 1)
658 {
659 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
660
661 // {skip} argument of searchpair() can be compiled if not empty
662 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100663 compile_string(isn, cctx, 0);
664 }
665 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
666 && cctx->ctx_instr.ga_len == instr_count + 1)
667 {
668 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
669
670 // {sub} argument of substitute() can be compiled if it starts
671 // with \=
672 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100673 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100674 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000675 }
676
677 if (*p != ',' && *skipwhite(p) == ',')
678 {
679 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
680 p = skipwhite(p);
681 }
682 if (*p == ',')
683 {
684 ++p;
685 if (*p != NUL && !VIM_ISWHITE(*p))
686 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
687 }
688 else
689 must_end = TRUE;
690 whitep = p;
691 p = skipwhite(p);
692 }
693failret:
694 emsg(_(e_missing_closing_paren));
695 return FAIL;
696}
697
698/*
699 * Compile a function call: name(arg1, arg2)
700 * "arg" points to "name", "arg + varlen" to the "(".
701 * "argcount_init" is 1 for "value->method()"
702 * Instructions:
703 * EVAL arg1
704 * EVAL arg2
705 * BCALL / DCALL / UCALL
706 */
707 static int
708compile_call(
709 char_u **arg,
710 size_t varlen,
711 cctx_T *cctx,
712 ppconst_T *ppconst,
713 int argcount_init)
714{
715 char_u *name = *arg;
716 char_u *p;
717 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100718 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000719 char_u fname_buf[FLEN_FIXED + 1];
720 char_u *tofree = NULL;
721 int error = FCERR_NONE;
722 ufunc_T *ufunc = NULL;
723 int res = FAIL;
724 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000725 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100726 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000727 imported_T *import;
728
729 if (varlen >= sizeof(namebuf))
730 {
731 semsg(_(e_name_too_long_str), name);
732 return FAIL;
733 }
734 vim_strncpy(namebuf, *arg, varlen);
735
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000736 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000737 if (import != NULL)
738 {
739 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
740 return FAIL;
741 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000742
743 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100744 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000745 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100746 if ((varlen == 3
747 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000748 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
749 {
750 char_u *s = skipwhite(*arg + varlen + 1);
751 typval_T argvars[2];
752 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100753 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000754
755 argvars[0].v_type = VAR_UNKNOWN;
756 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100757 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000758 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100759 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000760 s = skipwhite(s);
761 if (*s == ')' && argvars[0].v_type == VAR_STRING
762 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
763 || !is_has))
764 {
765 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
766
767 *arg = s + 1;
768 argvars[1].v_type = VAR_UNKNOWN;
769 tv->v_type = VAR_NUMBER;
770 tv->vval.v_number = 0;
771 if (is_has)
772 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100773 else if (is_len)
774 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000775 else
776 f_exists(argvars, tv);
777 clear_tv(&argvars[0]);
778 ++ppconst->pp_used;
779 return OK;
780 }
781 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100782 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000783 {
784 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
785 return FAIL;
786 }
787 }
788
789 if (generate_ppconst(cctx, ppconst) == FAIL)
790 return FAIL;
791
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000792 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
793
794 // We handle the "skip" argument of searchpair() and searchpairpos()
795 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100796 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
797 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
798 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
799 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
800 special_fn = CA_SEARCHPAIR;
801 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
802 special_fn = CA_SUBSTITUTE;
803 else
804 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000805
806 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100807 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000808 goto theend;
809
810 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
811 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
812 {
813 int idx;
814
815 // builtin function
816 idx = find_internal_func(name);
817 if (idx >= 0)
818 {
819 if (STRCMP(name, "flatten") == 0)
820 {
821 emsg(_(e_cannot_use_flatten_in_vim9_script));
822 goto theend;
823 }
824
825 if (STRCMP(name, "add") == 0 && argcount == 2)
826 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000827 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000828
829 // add() can be compiled to instructions if we know the type
830 if (type->tt_type == VAR_LIST)
831 {
832 // inline "add(list, item)" so that the type can be checked
833 res = generate_LISTAPPEND(cctx);
834 idx = -1;
835 }
836 else if (type->tt_type == VAR_BLOB)
837 {
838 // inline "add(blob, nr)" so that the type can be checked
839 res = generate_BLOBAPPEND(cctx);
840 idx = -1;
841 }
842 }
843
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100844 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
845 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100846 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100847 // May have the "D" or "R" flag, reserve a variable for a
848 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100849 if (get_defer_var_idx(cctx) == 0)
850 idx = -1;
851 }
852
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853 if (idx >= 0)
854 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
855 }
856 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100857 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000858 goto theend;
859 }
860
Bram Moolenaar62aec932022-01-29 21:45:34 +0000861 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
862
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000863 // An argument or local variable can be a function reference, this
864 // overrules a function name.
865 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
866 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
867 {
868 // If we can find the function by name generate the right call.
869 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000870 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000871 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000872 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000873 if (!func_is_global(ufunc))
874 {
875 res = generate_CALL(cctx, ufunc, argcount);
876 goto theend;
877 }
878 if (!has_g_namespace
879 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
880 {
881 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100882 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000883 goto theend;
884 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000885 }
886 }
887
888 // If the name is a variable, load it and use PCALL.
889 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000890 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000891 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000892 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000893 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
894 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000895 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000896
897 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
898 goto theend;
899 }
900
901 // If we can find a global function by name generate the right call.
902 if (ufunc != NULL)
903 {
904 res = generate_CALL(cctx, ufunc, argcount);
905 goto theend;
906 }
907
908 // A global function may be defined only later. Need to figure out at
909 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000910 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000911 res = generate_UCALL(cctx, name, argcount);
912 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100913 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000914
915theend:
916 vim_free(tofree);
917 return res;
918}
919
920// like NAMESPACE_CHAR but with 'a' and 'l'.
921#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
922
923/*
924 * Find the end of a variable or function name. Unlike find_name_end() this
925 * does not recognize magic braces.
926 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
927 * Return a pointer to just after the name. Equal to "arg" if there is no
928 * valid name.
929 */
930 char_u *
931to_name_end(char_u *arg, int use_namespace)
932{
933 char_u *p;
934
935 // Quick check for valid starting character.
936 if (!eval_isnamec1(*arg))
937 return arg;
938
939 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
940 // Include a namespace such as "s:var" and "v:var". But "n:" is not
941 // and can be used in slice "[n:]".
942 if (*p == ':' && (p != arg + 1
943 || !use_namespace
944 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
945 break;
946 return p;
947}
948
949/*
950 * Like to_name_end() but also skip over a list or dict constant.
951 * Also accept "<SNR>123_Func".
952 * This intentionally does not handle line continuation.
953 */
954 char_u *
955to_name_const_end(char_u *arg)
956{
957 char_u *p = arg;
958 typval_T rettv;
959
960 if (STRNCMP(p, "<SNR>", 5) == 0)
961 p = skipdigits(p + 5);
962 p = to_name_end(p, TRUE);
963 if (p == arg && *arg == '[')
964 {
965
966 // Can be "[1, 2, 3]->Func()".
967 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
968 p = arg;
969 }
970 return p;
971}
972
973/*
974 * parse a list: [expr, expr]
975 * "*arg" points to the '['.
976 * ppconst->pp_is_const is set if all items are a constant.
977 */
978 static int
979compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
980{
981 char_u *p = skipwhite(*arg + 1);
982 char_u *whitep = *arg + 1;
983 int count = 0;
984 int is_const;
985 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +0100986 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000987
988 for (;;)
989 {
990 if (may_get_next_line(whitep, &p, cctx) == FAIL)
991 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000992 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000993 return FAIL;
994 }
995 if (*p == ',')
996 {
997 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
998 return FAIL;
999 }
1000 if (*p == ']')
1001 {
1002 ++p;
1003 break;
1004 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001005 if (must_end)
1006 {
1007 semsg(_(e_missing_comma_in_list_str), p);
1008 return FAIL;
1009 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001010 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1011 return FAIL;
1012 if (!is_const)
1013 is_all_const = FALSE;
1014 ++count;
1015 if (*p == ',')
1016 {
1017 ++p;
1018 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1019 {
1020 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1021 return FAIL;
1022 }
1023 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001024 else
1025 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001026 whitep = p;
1027 p = skipwhite(p);
1028 }
1029 *arg = p;
1030
1031 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001032 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001033}
1034
1035/*
1036 * Parse a lambda: "(arg, arg) => expr"
1037 * "*arg" points to the '('.
1038 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1039 */
1040 static int
1041compile_lambda(char_u **arg, cctx_T *cctx)
1042{
1043 int r;
1044 typval_T rettv;
1045 ufunc_T *ufunc;
1046 evalarg_T evalarg;
1047
1048 init_evalarg(&evalarg);
1049 evalarg.eval_flags = EVAL_EVALUATE;
1050 evalarg.eval_cctx = cctx;
1051
1052 // Get the funcref in "rettv".
1053 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1054 if (r != OK)
1055 {
1056 clear_evalarg(&evalarg, NULL);
1057 return r;
1058 }
1059
1060 // "rettv" will now be a partial referencing the function.
1061 ufunc = rettv.vval.v_partial->pt_func;
1062 ++ufunc->uf_refcount;
1063 clear_tv(&rettv);
1064
1065 // Compile it here to get the return type. The return type is optional,
1066 // when it's missing use t_unknown. This is recognized in
1067 // compile_return().
1068 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1069 ufunc->uf_ret_type = &t_unknown;
1070 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1071
1072 // When the outer function is compiled for profiling or debugging, the
1073 // lambda may be called without profiling or debugging. Compile it here in
1074 // the right context.
1075 if (cctx->ctx_compile_type == CT_DEBUG
1076#ifdef FEAT_PROFILE
1077 || cctx->ctx_compile_type == CT_PROFILE
1078#endif
1079 )
1080 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1081
Bram Moolenaar139575d2022-03-15 19:29:30 +00001082 // if the outer function is not compiled for debugging or profiling, this
1083 // one might be
1084 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001085 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001086 compiletype_T compile_type = get_compile_type(ufunc);
1087
1088 if (compile_type != CT_NONE)
1089 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001090 }
1091
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001092 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1093 // "*arg" may point into it. Point into the original line to avoid a
1094 // dangling pointer.
1095 if (evalarg.eval_using_cmdline)
1096 {
1097 garray_T *gap = &evalarg.eval_tofree_ga;
1098 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1099
1100 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1101 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001102 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001103 }
1104
1105 clear_evalarg(&evalarg, NULL);
1106
1107 if (ufunc->uf_def_status == UF_COMPILED)
1108 {
1109 // The return type will now be known.
1110 set_function_type(ufunc);
1111
1112 // The function reference count will be 1. When the ISN_FUNCREF
1113 // instruction is deleted the reference count is decremented and the
1114 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001115 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001116 }
1117
1118 func_ptr_unref(ufunc);
1119 return FAIL;
1120}
1121
1122/*
1123 * Get a lambda and compile it. Uses Vim9 syntax.
1124 */
1125 int
1126get_lambda_tv_and_compile(
1127 char_u **arg,
1128 typval_T *rettv,
1129 int types_optional,
1130 evalarg_T *evalarg)
1131{
1132 int r;
1133 ufunc_T *ufunc;
1134 int save_sc_version = current_sctx.sc_version;
1135
1136 // Get the funcref in "rettv".
1137 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1138 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1139 current_sctx.sc_version = save_sc_version;
1140 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001141 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001142
1143 // "rettv" will now be a partial referencing the function.
1144 ufunc = rettv->vval.v_partial->pt_func;
1145
1146 // Compile it here to get the return type. The return type is optional,
1147 // when it's missing use t_unknown. This is recognized in
1148 // compile_return().
1149 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1150 ufunc->uf_ret_type = &t_unknown;
1151 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1152
1153 if (ufunc->uf_def_status == UF_COMPILED)
1154 {
1155 // The return type will now be known.
1156 set_function_type(ufunc);
1157 return OK;
1158 }
1159 clear_tv(rettv);
1160 return FAIL;
1161}
1162
1163/*
1164 * parse a dict: {key: val, [key]: val}
1165 * "*arg" points to the '{'.
1166 * ppconst->pp_is_const is set if all item values are a constant.
1167 */
1168 static int
1169compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1170{
1171 garray_T *instr = &cctx->ctx_instr;
1172 int count = 0;
1173 dict_T *d = dict_alloc();
1174 dictitem_T *item;
1175 char_u *whitep = *arg + 1;
1176 char_u *p;
1177 int is_const;
1178 int is_all_const = TRUE; // reset when non-const encountered
1179
1180 if (d == NULL)
1181 return FAIL;
1182 if (generate_ppconst(cctx, ppconst) == FAIL)
1183 return FAIL;
1184 for (;;)
1185 {
1186 char_u *key = NULL;
1187
1188 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1189 {
1190 *arg = NULL;
1191 goto failret;
1192 }
1193
1194 if (**arg == '}')
1195 break;
1196
1197 if (**arg == '[')
1198 {
1199 isn_T *isn;
1200
1201 // {[expr]: value} uses an evaluated key.
1202 *arg = skipwhite(*arg + 1);
1203 if (compile_expr0(arg, cctx) == FAIL)
1204 return FAIL;
1205 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1206 if (isn->isn_type == ISN_PUSHNR)
1207 {
1208 char buf[NUMBUFLEN];
1209
1210 // Convert to string at compile time.
1211 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1212 isn->isn_type = ISN_PUSHS;
1213 isn->isn_arg.string = vim_strsave((char_u *)buf);
1214 }
1215 if (isn->isn_type == ISN_PUSHS)
1216 key = isn->isn_arg.string;
1217 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1218 return FAIL;
1219 *arg = skipwhite(*arg);
1220 if (**arg != ']')
1221 {
1222 emsg(_(e_missing_matching_bracket_after_dict_key));
1223 return FAIL;
1224 }
1225 ++*arg;
1226 }
1227 else
1228 {
1229 // {"name": value},
1230 // {'name': value},
1231 // {name: value} use "name" as a literal key
1232 key = get_literal_key(arg);
1233 if (key == NULL)
1234 return FAIL;
1235 if (generate_PUSHS(cctx, &key) == FAIL)
1236 return FAIL;
1237 }
1238
1239 // Check for duplicate keys, if using string keys.
1240 if (key != NULL)
1241 {
1242 item = dict_find(d, key, -1);
1243 if (item != NULL)
1244 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001245 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001246 goto failret;
1247 }
1248 item = dictitem_alloc(key);
1249 if (item != NULL)
1250 {
1251 item->di_tv.v_type = VAR_UNKNOWN;
1252 item->di_tv.v_lock = 0;
1253 if (dict_add(d, item) == FAIL)
1254 dictitem_free(item);
1255 }
1256 }
1257
1258 if (**arg != ':')
1259 {
1260 if (*skipwhite(*arg) == ':')
1261 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1262 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001263 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001264 return FAIL;
1265 }
1266 whitep = *arg + 1;
1267 if (!IS_WHITE_OR_NUL(*whitep))
1268 {
1269 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1270 return FAIL;
1271 }
1272
1273 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1274 {
1275 *arg = NULL;
1276 goto failret;
1277 }
1278
1279 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1280 return FAIL;
1281 if (!is_const)
1282 is_all_const = FALSE;
1283 ++count;
1284
1285 whitep = *arg;
1286 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1287 {
1288 *arg = NULL;
1289 goto failret;
1290 }
1291 if (**arg == '}')
1292 break;
1293 if (**arg != ',')
1294 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001295 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001296 goto failret;
1297 }
1298 if (IS_WHITE_OR_NUL(*whitep))
1299 {
1300 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1301 return FAIL;
1302 }
1303 whitep = *arg + 1;
1304 if (!IS_WHITE_OR_NUL(*whitep))
1305 {
1306 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1307 return FAIL;
1308 }
1309 *arg = skipwhite(whitep);
1310 }
1311
1312 *arg = *arg + 1;
1313
1314 // Allow for following comment, after at least one space.
1315 p = skipwhite(*arg);
1316 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1317 *arg += STRLEN(*arg);
1318
1319 dict_unref(d);
1320 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001321 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001322
1323failret:
1324 if (*arg == NULL)
1325 {
1326 semsg(_(e_missing_dict_end), _("[end of lines]"));
1327 *arg = (char_u *)"";
1328 }
1329 dict_unref(d);
1330 return FAIL;
1331}
1332
1333/*
1334 * Compile "&option".
1335 */
1336 static int
1337compile_get_option(char_u **arg, cctx_T *cctx)
1338{
1339 typval_T rettv;
1340 char_u *start = *arg;
1341 int ret;
1342
1343 // parse the option and get the current value to get the type.
1344 rettv.v_type = VAR_UNKNOWN;
1345 ret = eval_option(arg, &rettv, TRUE);
1346 if (ret == OK)
1347 {
1348 // include the '&' in the name, eval_option() expects it.
1349 char_u *name = vim_strnsave(start, *arg - start);
1350 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1351 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1352
1353 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1354 vim_free(name);
1355 }
1356 clear_tv(&rettv);
1357
1358 return ret;
1359}
1360
1361/*
1362 * Compile "$VAR".
1363 */
1364 static int
1365compile_get_env(char_u **arg, cctx_T *cctx)
1366{
1367 char_u *start = *arg;
1368 int len;
1369 int ret;
1370 char_u *name;
1371
1372 ++*arg;
1373 len = get_env_len(arg);
1374 if (len == 0)
1375 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001376 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001377 return FAIL;
1378 }
1379
1380 // include the '$' in the name, eval_env_var() expects it.
1381 name = vim_strnsave(start, len + 1);
1382 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1383 vim_free(name);
1384 return ret;
1385}
1386
1387/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001388 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001389 */
1390 static int
1391compile_interp_string(char_u **arg, cctx_T *cctx)
1392{
1393 typval_T tv;
1394 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001395 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001396 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001397 int count = 0;
1398 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001399
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001400 // *arg is on the '$' character, move it to the first string character.
1401 ++*arg;
1402 quote = **arg;
1403 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001404
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001405 for (;;)
1406 {
1407 // Get the string up to the matching quote or to a single '{'.
1408 // "arg" is advanced to either the quote or the '{'.
1409 if (quote == '"')
1410 ret = eval_string(arg, &tv, evaluate, TRUE);
1411 else
1412 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1413 if (ret == FAIL)
1414 break;
1415 if (evaluate)
1416 {
1417 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1418 || (**arg != '{' && count == 0))
1419 {
1420 // generate non-empty string or empty string if it's the only
1421 // one
1422 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1423 return FAIL;
1424 tv.vval.v_string = NULL; // don't free it now
1425 ++count;
1426 }
1427 clear_tv(&tv);
1428 }
1429
1430 if (**arg != '{')
1431 {
1432 // found terminating quote
1433 ++*arg;
1434 break;
1435 }
1436
1437 p = compile_one_expr_in_str(*arg, cctx);
1438 if (p == NULL)
1439 {
1440 ret = FAIL;
1441 break;
1442 }
1443 ++count;
1444 *arg = p;
1445 }
LemonBoy2eaef102022-05-06 13:14:50 +01001446
1447 if (ret == FAIL || !evaluate)
1448 return ret;
1449
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001450 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1451 if (count > 1)
1452 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001453
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001454 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001455}
1456
1457/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001458 * Compile "@r".
1459 */
1460 static int
1461compile_get_register(char_u **arg, cctx_T *cctx)
1462{
1463 int ret;
1464
1465 ++*arg;
1466 if (**arg == NUL)
1467 {
1468 semsg(_(e_syntax_error_at_str), *arg - 1);
1469 return FAIL;
1470 }
1471 if (!valid_yank_reg(**arg, FALSE))
1472 {
1473 emsg_invreg(**arg);
1474 return FAIL;
1475 }
1476 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1477 ++*arg;
1478 return ret;
1479}
1480
1481/*
1482 * Apply leading '!', '-' and '+' to constant "rettv".
1483 * When "numeric_only" is TRUE do not apply '!'.
1484 */
1485 static int
1486apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1487{
1488 char_u *p = *end;
1489
1490 // this works from end to start
1491 while (p > start)
1492 {
1493 --p;
1494 if (*p == '-' || *p == '+')
1495 {
1496 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001497 if (rettv->v_type == VAR_FLOAT)
1498 {
1499 if (*p == '-')
1500 rettv->vval.v_float = -rettv->vval.v_float;
1501 }
1502 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001503 {
1504 varnumber_T val;
1505 int error = FALSE;
1506
1507 // tv_get_number_chk() accepts a string, but we don't want that
1508 // here
1509 if (check_not_string(rettv) == FAIL)
1510 return FAIL;
1511 val = tv_get_number_chk(rettv, &error);
1512 clear_tv(rettv);
1513 if (error)
1514 return FAIL;
1515 if (*p == '-')
1516 val = -val;
1517 rettv->v_type = VAR_NUMBER;
1518 rettv->vval.v_number = val;
1519 }
1520 }
1521 else if (numeric_only)
1522 {
1523 ++p;
1524 break;
1525 }
1526 else if (*p == '!')
1527 {
1528 int v = tv2bool(rettv);
1529
1530 // '!' is permissive in the type.
1531 clear_tv(rettv);
1532 rettv->v_type = VAR_BOOL;
1533 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1534 }
1535 }
1536 *end = p;
1537 return OK;
1538}
1539
1540/*
1541 * Recognize v: variables that are constants and set "rettv".
1542 */
1543 static void
1544get_vim_constant(char_u **arg, typval_T *rettv)
1545{
1546 if (STRNCMP(*arg, "v:true", 6) == 0)
1547 {
1548 rettv->v_type = VAR_BOOL;
1549 rettv->vval.v_number = VVAL_TRUE;
1550 *arg += 6;
1551 }
1552 else if (STRNCMP(*arg, "v:false", 7) == 0)
1553 {
1554 rettv->v_type = VAR_BOOL;
1555 rettv->vval.v_number = VVAL_FALSE;
1556 *arg += 7;
1557 }
1558 else if (STRNCMP(*arg, "v:null", 6) == 0)
1559 {
1560 rettv->v_type = VAR_SPECIAL;
1561 rettv->vval.v_number = VVAL_NULL;
1562 *arg += 6;
1563 }
1564 else if (STRNCMP(*arg, "v:none", 6) == 0)
1565 {
1566 rettv->v_type = VAR_SPECIAL;
1567 rettv->vval.v_number = VVAL_NONE;
1568 *arg += 6;
1569 }
1570}
1571
1572 exprtype_T
1573get_compare_type(char_u *p, int *len, int *type_is)
1574{
1575 exprtype_T type = EXPR_UNKNOWN;
1576 int i;
1577
1578 switch (p[0])
1579 {
1580 case '=': if (p[1] == '=')
1581 type = EXPR_EQUAL;
1582 else if (p[1] == '~')
1583 type = EXPR_MATCH;
1584 break;
1585 case '!': if (p[1] == '=')
1586 type = EXPR_NEQUAL;
1587 else if (p[1] == '~')
1588 type = EXPR_NOMATCH;
1589 break;
1590 case '>': if (p[1] != '=')
1591 {
1592 type = EXPR_GREATER;
1593 *len = 1;
1594 }
1595 else
1596 type = EXPR_GEQUAL;
1597 break;
1598 case '<': if (p[1] != '=')
1599 {
1600 type = EXPR_SMALLER;
1601 *len = 1;
1602 }
1603 else
1604 type = EXPR_SEQUAL;
1605 break;
1606 case 'i': if (p[1] == 's')
1607 {
1608 // "is" and "isnot"; but not a prefix of a name
1609 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1610 *len = 5;
1611 i = p[*len];
1612 if (!isalnum(i) && i != '_')
1613 {
1614 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1615 *type_is = TRUE;
1616 }
1617 }
1618 break;
1619 }
1620 return type;
1621}
1622
1623/*
1624 * Skip over an expression, ignoring most errors.
1625 */
1626 void
1627skip_expr_cctx(char_u **arg, cctx_T *cctx)
1628{
1629 evalarg_T evalarg;
1630
1631 init_evalarg(&evalarg);
1632 evalarg.eval_cctx = cctx;
1633 skip_expr(arg, &evalarg);
1634 clear_evalarg(&evalarg, NULL);
1635}
1636
1637/*
1638 * Check that the top of the type stack has a type that can be used as a
1639 * condition. Give an error and return FAIL if not.
1640 */
1641 int
1642bool_on_stack(cctx_T *cctx)
1643{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001644 type_T *type;
1645
Bram Moolenaar078a4612022-01-04 15:17:03 +00001646 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001647 if (type == &t_bool)
1648 return OK;
1649
Bram Moolenaar4913d422022-10-17 13:13:32 +01001650 if (type->tt_type == VAR_ANY
1651 || type->tt_type == VAR_UNKNOWN
1652 || type->tt_type == VAR_NUMBER
1653 || type == &t_number_bool
1654 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001655 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1656 // This requires a runtime type check.
1657 return generate_COND2BOOL(cctx);
1658
1659 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1660}
1661
1662/*
1663 * Give the "white on both sides" error, taking the operator from "p[len]".
1664 */
1665 void
1666error_white_both(char_u *op, int len)
1667{
1668 char_u buf[10];
1669
1670 vim_strncpy(buf, op, len);
1671 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1672}
1673
1674/*
1675 * Compile code to apply '-', '+' and '!'.
1676 * When "numeric_only" is TRUE do not apply '!'.
1677 */
1678 static int
1679compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1680{
1681 char_u *p = *end;
1682
1683 // this works from end to start
1684 while (p > start)
1685 {
1686 --p;
1687 while (VIM_ISWHITE(*p))
1688 --p;
1689 if (*p == '-' || *p == '+')
1690 {
1691 int negate = *p == '-';
1692 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001693 type_T *type;
1694
Bram Moolenaar078a4612022-01-04 15:17:03 +00001695 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001696 if (type != &t_float && need_type(type, &t_number,
1697 -1, 0, cctx, FALSE, FALSE) == FAIL)
1698 return FAIL;
1699
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001700 // only '-' has an effect, for '+' we only check the type
1701 if (negate)
1702 {
1703 isn = generate_instr(cctx, ISN_NEGATENR);
1704 if (isn == NULL)
1705 return FAIL;
1706 }
1707 }
1708 else if (numeric_only)
1709 {
1710 ++p;
1711 break;
1712 }
1713 else
1714 {
1715 int invert = *p == '!';
1716
1717 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1718 {
1719 if (p[-1] == '!')
1720 invert = !invert;
1721 --p;
1722 }
1723 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1724 return FAIL;
1725 }
1726 }
1727 *end = p;
1728 return OK;
1729}
1730
1731/*
1732 * Compile "(expression)": recursive!
1733 * Return FAIL/OK.
1734 */
1735 static int
1736compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1737{
1738 int ret;
1739 char_u *p = *arg + 1;
1740
1741 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1742 return FAIL;
1743 if (ppconst->pp_used <= PPSIZE - 10)
1744 {
1745 ret = compile_expr1(arg, cctx, ppconst);
1746 }
1747 else
1748 {
1749 // Not enough space in ppconst, flush constants.
1750 if (generate_ppconst(cctx, ppconst) == FAIL)
1751 return FAIL;
1752 ret = compile_expr0(arg, cctx);
1753 }
1754 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1755 return FAIL;
1756 if (**arg == ')')
1757 ++*arg;
1758 else if (ret == OK)
1759 {
1760 emsg(_(e_missing_closing_paren));
1761 ret = FAIL;
1762 }
1763 return ret;
1764}
1765
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001766static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001767
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001768/*
1769 * Compile whatever comes after "name" or "name()".
1770 * Advances "*arg" only when something was recognized.
1771 */
1772 static int
1773compile_subscript(
1774 char_u **arg,
1775 cctx_T *cctx,
1776 char_u *start_leader,
1777 char_u **end_leader,
1778 ppconst_T *ppconst)
1779{
1780 char_u *name_start = *end_leader;
1781 int keeping_dict = FALSE;
1782
1783 for (;;)
1784 {
1785 char_u *p = skipwhite(*arg);
1786
1787 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1788 {
1789 char_u *next = peek_next_line_from_context(cctx);
1790
1791 // If a following line starts with "->{" or "->X" advance to that
1792 // line, so that a line break before "->" is allowed.
1793 // Also if a following line starts with ".x".
1794 if (next != NULL &&
1795 ((next[0] == '-' && next[1] == '>'
1796 && (next[2] == '{'
1797 || ASCII_ISALPHA(*skipwhite(next + 2))))
1798 || (next[0] == '.' && eval_isdictc(next[1]))))
1799 {
1800 next = next_line_from_context(cctx, TRUE);
1801 if (next == NULL)
1802 return FAIL;
1803 *arg = next;
1804 p = skipwhite(*arg);
1805 }
1806 }
1807
1808 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1809 // is not a function call.
1810 if (**arg == '(')
1811 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001812 type_T *type;
1813 int argcount = 0;
1814
1815 if (generate_ppconst(cctx, ppconst) == FAIL)
1816 return FAIL;
1817 ppconst->pp_is_const = FALSE;
1818
1819 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001820 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001821
1822 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001823 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001824 return FAIL;
1825 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1826 return FAIL;
1827 if (keeping_dict)
1828 {
1829 keeping_dict = FALSE;
1830 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1831 return FAIL;
1832 }
1833 }
1834 else if (*p == '-' && p[1] == '>')
1835 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001836 char_u *pstart = p;
1837 int alt;
1838 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001839
Bram Moolenaarc7349932022-01-16 20:59:39 +00001840 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001841 if (generate_ppconst(cctx, ppconst) == FAIL)
1842 return FAIL;
1843 ppconst->pp_is_const = FALSE;
1844
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001845 // Apply the '!', '-' and '+' first:
1846 // -1.0->func() works like (-1.0)->func()
1847 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1848 return FAIL;
1849
1850 p += 2;
1851 *arg = skipwhite(p);
1852 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001853
1854 // Three alternatives handled here:
1855 // 1. "base->name(" only a name, use compile_call()
1856 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1857 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001858 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001859 alt = 2;
1860 else
1861 {
1862 // alternative 1 or 3
1863 p = *arg;
1864 if (!eval_isnamec1(*p))
1865 {
1866 semsg(_(e_trailing_characters_str), pstart);
1867 return FAIL;
1868 }
1869 if (ASCII_ISALPHA(*p) && p[1] == ':')
1870 p += 2;
1871 for ( ; eval_isnamec(*p); ++p)
1872 ;
1873 if (*p == '(')
1874 {
1875 // alternative 1
1876 alt = 1;
1877 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1878 return FAIL;
1879 }
1880 else
1881 {
1882 // Must be alternative 3, find the "(". Only works within
1883 // one line.
1884 alt = 3;
1885 paren = vim_strchr(p, '(');
1886 if (paren == NULL)
1887 {
1888 semsg(_(e_missing_parenthesis_str), *arg);
1889 return FAIL;
1890 }
1891 }
1892 }
1893
1894 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001895 {
1896 int argcount = 1;
1897 garray_T *stack = &cctx->ctx_type_stack;
1898 int type_idx_start = stack->ga_len;
1899 type_T *type;
1900 int expr_isn_start = cctx->ctx_instr.ga_len;
1901 int expr_isn_end;
1902 int arg_isn_count;
1903
Bram Moolenaarc7349932022-01-16 20:59:39 +00001904 if (alt == 2)
1905 {
1906 // Funcref call: list->(Refs[2])(arg)
1907 // or lambda: list->((arg) => expr)(arg)
1908 //
1909 // Fist compile the function expression.
1910 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1911 return FAIL;
1912 }
1913 else
1914 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001915 int fail;
1916 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001917 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001918
Bram Moolenaarc7349932022-01-16 20:59:39 +00001919 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001920
1921 // instead of using LOADG for "import.Func" use PUSHFUNC
1922 ++paren_follows_after_expr;
1923
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001924 // do not look in the next line
1925 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001926
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001927 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001928 || *skipwhite(*arg) != NUL;
1929 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001930 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001931 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001932
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001933 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001934 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001935 if (did_emsg == prev_did_emsg)
1936 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001937 return FAIL;
1938 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001939 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001940
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001941 // Compile the arguments.
1942 if (**arg != '(')
1943 {
1944 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001945 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001946 else
1947 semsg(_(e_missing_parenthesis_str), *arg);
1948 return FAIL;
1949 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001950
1951 // Remember the next instruction index, where the instructions
1952 // for arguments are being written.
1953 expr_isn_end = cctx->ctx_instr.ga_len;
1954
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001955 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001956 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
1957 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001958 return FAIL;
1959
1960 // Move the instructions for the arguments to before the
1961 // instructions of the expression and move the type of the
1962 // expression after the argument types. This is what ISN_PCALL
1963 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001964 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1965 if (arg_isn_count > 0)
1966 {
1967 int expr_isn_count = expr_isn_end - expr_isn_start;
1968 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001969 type_T *decl_type;
1970 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001971
1972 if (isn == NULL)
1973 return FAIL;
1974 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1975 + expr_isn_start,
1976 sizeof(isn_T) * expr_isn_count);
1977 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1978 + expr_isn_start,
1979 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1980 sizeof(isn_T) * arg_isn_count);
1981 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1982 + expr_isn_start + arg_isn_count,
1983 isn, sizeof(isn_T) * expr_isn_count);
1984 vim_free(isn);
1985
Bram Moolenaar078a4612022-01-04 15:17:03 +00001986 typep = ((type2_T *)stack->ga_data) + type_idx_start;
1987 type = typep->type_curr;
1988 decl_type = typep->type_decl;
1989 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
1990 ((type2_T *)stack->ga_data) + type_idx_start + 1,
1991 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001992 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00001993 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
1994 typep->type_curr = type;
1995 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001996 }
1997
Bram Moolenaar078a4612022-01-04 15:17:03 +00001998 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001999 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2000 return FAIL;
2001 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002002
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002003 if (keeping_dict)
2004 {
2005 keeping_dict = FALSE;
2006 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2007 return FAIL;
2008 }
2009 }
2010 else if (**arg == '[')
2011 {
2012 int is_slice = FALSE;
2013
2014 // list index: list[123]
2015 // dict member: dict[key]
2016 // string index: text[123]
2017 // blob index: blob[123]
2018 if (generate_ppconst(cctx, ppconst) == FAIL)
2019 return FAIL;
2020 ppconst->pp_is_const = FALSE;
2021
2022 ++p;
2023 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2024 return FAIL;
2025 if (**arg == ':')
2026 {
2027 // missing first index is equal to zero
2028 generate_PUSHNR(cctx, 0);
2029 }
2030 else
2031 {
2032 if (compile_expr0(arg, cctx) == FAIL)
2033 return FAIL;
2034 if (**arg == ':')
2035 {
2036 semsg(_(e_white_space_required_before_and_after_str_at_str),
2037 ":", *arg);
2038 return FAIL;
2039 }
2040 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2041 return FAIL;
2042 *arg = skipwhite(*arg);
2043 }
2044 if (**arg == ':')
2045 {
2046 is_slice = TRUE;
2047 ++*arg;
2048 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2049 {
2050 semsg(_(e_white_space_required_before_and_after_str_at_str),
2051 ":", *arg);
2052 return FAIL;
2053 }
2054 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2055 return FAIL;
2056 if (**arg == ']')
2057 // missing second index is equal to end of string
2058 generate_PUSHNR(cctx, -1);
2059 else
2060 {
2061 if (compile_expr0(arg, cctx) == FAIL)
2062 return FAIL;
2063 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2064 return FAIL;
2065 *arg = skipwhite(*arg);
2066 }
2067 }
2068
2069 if (**arg != ']')
2070 {
2071 emsg(_(e_missing_closing_square_brace));
2072 return FAIL;
2073 }
2074 *arg = *arg + 1;
2075
2076 if (keeping_dict)
2077 {
2078 keeping_dict = FALSE;
2079 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2080 return FAIL;
2081 }
2082 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2083 return FAIL;
2084 }
2085 else if (*p == '.' && p[1] != '.')
2086 {
2087 // dictionary member: dict.name
2088 if (generate_ppconst(cctx, ppconst) == FAIL)
2089 return FAIL;
2090 ppconst->pp_is_const = FALSE;
2091
2092 *arg = p + 1;
2093 if (IS_WHITE_OR_NUL(**arg))
2094 {
2095 emsg(_(e_missing_name_after_dot));
2096 return FAIL;
2097 }
2098 p = *arg;
2099 if (eval_isdictc(*p))
2100 while (eval_isnamec(*p))
2101 MB_PTR_ADV(p);
2102 if (p == *arg)
2103 {
2104 semsg(_(e_syntax_error_at_str), *arg);
2105 return FAIL;
2106 }
2107 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2108 return FAIL;
2109 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2110 return FAIL;
2111 keeping_dict = TRUE;
2112 *arg = p;
2113 }
2114 else
2115 break;
2116 }
2117
2118 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2119 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002120 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2121 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002122 return FAIL;
2123
2124 return OK;
2125}
2126
2127/*
2128 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2129 * "arg" is advanced until after the expression, skipping white space.
2130 *
2131 * If the value is a constant "ppconst->pp_used" will be non-zero.
2132 * Before instructions are generated, any values in "ppconst" will generated.
2133 *
2134 * This is the compiling equivalent of eval1(), eval2(), etc.
2135 */
2136
2137/*
2138 * number number constant
2139 * 0zFFFFFFFF Blob constant
2140 * "string" string constant
2141 * 'string' literal string constant
2142 * &option-name option value
2143 * @r register contents
2144 * identifier variable value
2145 * function() function call
2146 * $VAR environment variable
2147 * (expression) nested expression
2148 * [expr, expr] List
2149 * {key: val, [key]: val} Dictionary
2150 *
2151 * Also handle:
2152 * ! in front logical NOT
2153 * - in front unary minus
2154 * + in front unary plus (ignored)
2155 * trailing (arg) funcref/partial call
2156 * trailing [] subscript in String or List
2157 * trailing .name entry in Dictionary
2158 * trailing ->name() method call
2159 */
2160 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002161compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002162 char_u **arg,
2163 cctx_T *cctx,
2164 ppconst_T *ppconst)
2165{
2166 char_u *start_leader, *end_leader;
2167 int ret = OK;
2168 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2169 int used_before = ppconst->pp_used;
2170
2171 ppconst->pp_is_const = FALSE;
2172
2173 /*
2174 * Skip '!', '-' and '+' characters. They are handled later.
2175 */
2176 start_leader = *arg;
2177 if (eval_leader(arg, TRUE) == FAIL)
2178 return FAIL;
2179 end_leader = *arg;
2180
2181 rettv->v_type = VAR_UNKNOWN;
2182 switch (**arg)
2183 {
2184 /*
2185 * Number constant.
2186 */
2187 case '0': // also for blob starting with 0z
2188 case '1':
2189 case '2':
2190 case '3':
2191 case '4':
2192 case '5':
2193 case '6':
2194 case '7':
2195 case '8':
2196 case '9':
2197 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2198 return FAIL;
2199 // Apply "-" and "+" just before the number now, right to
2200 // left. Matters especially when "->" follows. Stops at
2201 // '!'.
2202 if (apply_leader(rettv, TRUE,
2203 start_leader, &end_leader) == FAIL)
2204 {
2205 clear_tv(rettv);
2206 return FAIL;
2207 }
2208 break;
2209
2210 /*
2211 * String constant: "string".
2212 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002213 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002214 return FAIL;
2215 break;
2216
2217 /*
2218 * Literal string constant: 'str''ing'.
2219 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002220 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002221 return FAIL;
2222 break;
2223
2224 /*
2225 * Constant Vim variable.
2226 */
2227 case 'v': get_vim_constant(arg, rettv);
2228 ret = NOTDONE;
2229 break;
2230
2231 /*
2232 * "true" constant
2233 */
2234 case 't': if (STRNCMP(*arg, "true", 4) == 0
2235 && !eval_isnamec((*arg)[4]))
2236 {
2237 *arg += 4;
2238 rettv->v_type = VAR_BOOL;
2239 rettv->vval.v_number = VVAL_TRUE;
2240 }
2241 else
2242 ret = NOTDONE;
2243 break;
2244
2245 /*
2246 * "false" constant
2247 */
2248 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2249 && !eval_isnamec((*arg)[5]))
2250 {
2251 *arg += 5;
2252 rettv->v_type = VAR_BOOL;
2253 rettv->vval.v_number = VVAL_FALSE;
2254 }
2255 else
2256 ret = NOTDONE;
2257 break;
2258
2259 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002260 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002261 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002262 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002263 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002264 char_u *p = *arg + 4;
2265 int len;
2266
2267 for (len = 0; eval_isnamec(p[len]); ++len)
2268 ;
2269 ret = handle_predefined(*arg, len + 4, rettv);
2270 if (ret == FAIL)
2271 ret = NOTDONE;
2272 else
2273 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002274 }
2275 else
2276 ret = NOTDONE;
2277 break;
2278
2279 /*
2280 * List: [expr, expr]
2281 */
2282 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2283 return FAIL;
2284 ret = compile_list(arg, cctx, ppconst);
2285 break;
2286
2287 /*
2288 * Dictionary: {'key': val, 'key': val}
2289 */
2290 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2291 return FAIL;
2292 ret = compile_dict(arg, cctx, ppconst);
2293 break;
2294
2295 /*
2296 * Option value: &name
2297 */
2298 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2299 return FAIL;
2300 ret = compile_get_option(arg, cctx);
2301 break;
2302
2303 /*
2304 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002305 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002306 */
2307 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2308 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002309 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2310 ret = compile_interp_string(arg, cctx);
2311 else
2312 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002313 break;
2314
2315 /*
2316 * Register contents: @r.
2317 */
2318 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2319 return FAIL;
2320 ret = compile_get_register(arg, cctx);
2321 break;
2322 /*
2323 * nested expression: (expression).
2324 * lambda: (arg, arg) => expr
2325 * funcref: (arg, arg) => { statement }
2326 */
2327 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2328 ret = compile_lambda(arg, cctx);
2329 if (ret == NOTDONE)
2330 ret = compile_parenthesis(arg, cctx, ppconst);
2331 break;
2332
2333 default: ret = NOTDONE;
2334 break;
2335 }
2336 if (ret == FAIL)
2337 return FAIL;
2338
2339 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2340 {
2341 if (cctx->ctx_skip == SKIP_YES)
2342 clear_tv(rettv);
2343 else
2344 // A constant expression can possibly be handled compile time,
2345 // return the value instead of generating code.
2346 ++ppconst->pp_used;
2347 }
2348 else if (ret == NOTDONE)
2349 {
2350 char_u *p;
2351 int r;
2352
2353 if (!eval_isnamec1(**arg))
2354 {
2355 if (!vim9_bad_comment(*arg))
2356 {
2357 if (ends_excmd(*skipwhite(*arg)))
2358 semsg(_(e_empty_expression_str), *arg);
2359 else
2360 semsg(_(e_name_expected_str), *arg);
2361 }
2362 return FAIL;
2363 }
2364
2365 // "name" or "name()"
2366 p = to_name_end(*arg, TRUE);
2367 if (p - *arg == (size_t)1 && **arg == '_')
2368 {
2369 emsg(_(e_cannot_use_underscore_here));
2370 return FAIL;
2371 }
2372
2373 if (*p == '(')
2374 {
2375 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2376 }
2377 else
2378 {
2379 if (cctx->ctx_skip != SKIP_YES
2380 && generate_ppconst(cctx, ppconst) == FAIL)
2381 return FAIL;
2382 r = compile_load(arg, p, cctx, TRUE, TRUE);
2383 }
2384 if (r == FAIL)
2385 return FAIL;
2386 }
2387
2388 // Handle following "[]", ".member", etc.
2389 // Then deal with prefixed '-', '+' and '!', if not done already.
2390 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2391 ppconst) == FAIL)
2392 return FAIL;
2393 if (ppconst->pp_used > 0)
2394 {
2395 // apply the '!', '-' and '+' before the constant
2396 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2397 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2398 return FAIL;
2399 return OK;
2400 }
2401 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2402 return FAIL;
2403 return OK;
2404}
2405
2406/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002407 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002408 */
2409 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002410compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002411{
2412 type_T *want_type = NULL;
2413
2414 // Recognize <type>
2415 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2416 {
2417 ++*arg;
2418 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2419 if (want_type == NULL)
2420 return FAIL;
2421
2422 if (**arg != '>')
2423 {
2424 if (*skipwhite(*arg) == '>')
2425 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2426 else
2427 emsg(_(e_missing_gt));
2428 return FAIL;
2429 }
2430 ++*arg;
2431 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2432 return FAIL;
2433 }
2434
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002435 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002436 return FAIL;
2437
2438 if (want_type != NULL)
2439 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002440 type_T *actual;
2441 where_T where = WHERE_INIT;
2442
2443 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002444 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002445 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002446 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002447 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2448 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002449 return FAIL;
2450 }
2451 }
2452
2453 return OK;
2454}
2455
2456/*
2457 * * number multiplication
2458 * / number division
2459 * % number modulo
2460 */
2461 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002462compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002463{
2464 char_u *op;
2465 char_u *next;
2466 int ppconst_used = ppconst->pp_used;
2467
2468 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002469 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002470 return FAIL;
2471
2472 /*
2473 * Repeat computing, until no "*", "/" or "%" is following.
2474 */
2475 for (;;)
2476 {
2477 op = may_peek_next_line(cctx, *arg, &next);
2478 if (*op != '*' && *op != '/' && *op != '%')
2479 break;
2480 if (next != NULL)
2481 {
2482 *arg = next_line_from_context(cctx, TRUE);
2483 op = skipwhite(*arg);
2484 }
2485
2486 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2487 {
2488 error_white_both(op, 1);
2489 return FAIL;
2490 }
2491 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2492 return FAIL;
2493
2494 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002495 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002496 return FAIL;
2497
2498 if (ppconst->pp_used == ppconst_used + 2
2499 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2500 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2501 {
2502 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2503 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2504 varnumber_T res = 0;
2505 int failed = FALSE;
2506
2507 // both are numbers: compute the result
2508 switch (*op)
2509 {
2510 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2511 break;
2512 case '/': res = num_divide(tv1->vval.v_number,
2513 tv2->vval.v_number, &failed);
2514 break;
2515 case '%': res = num_modulus(tv1->vval.v_number,
2516 tv2->vval.v_number, &failed);
2517 break;
2518 }
2519 if (failed)
2520 return FAIL;
2521 tv1->vval.v_number = res;
2522 --ppconst->pp_used;
2523 }
2524 else
2525 {
2526 generate_ppconst(cctx, ppconst);
2527 generate_two_op(cctx, op);
2528 }
2529 }
2530
2531 return OK;
2532}
2533
2534/*
2535 * + number addition or list/blobl concatenation
2536 * - number subtraction
2537 * .. string concatenation
2538 */
2539 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002540compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002541{
2542 char_u *op;
2543 char_u *next;
2544 int oplen;
2545 int ppconst_used = ppconst->pp_used;
2546
2547 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002548 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002549 return FAIL;
2550
2551 /*
2552 * Repeat computing, until no "+", "-" or ".." is following.
2553 */
2554 for (;;)
2555 {
2556 op = may_peek_next_line(cctx, *arg, &next);
2557 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2558 break;
2559 if (op[0] == op[1] && *op != '.' && next)
2560 // Finding "++" or "--" on the next line is a separate command.
2561 // But ".." is concatenation.
2562 break;
2563 oplen = (*op == '.' ? 2 : 1);
2564 if (next != NULL)
2565 {
2566 *arg = next_line_from_context(cctx, TRUE);
2567 op = skipwhite(*arg);
2568 }
2569
2570 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2571 {
2572 error_white_both(op, oplen);
2573 return FAIL;
2574 }
2575
2576 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2577 return FAIL;
2578
2579 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002580 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002581 return FAIL;
2582
2583 if (ppconst->pp_used == ppconst_used + 2
2584 && (*op == '.'
2585 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2586 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2587 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2588 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2589 {
2590 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2591 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2592
2593 // concat/subtract/add constant numbers
2594 if (*op == '+')
2595 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2596 else if (*op == '-')
2597 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2598 else
2599 {
2600 // concatenate constant strings
2601 char_u *s1 = tv1->vval.v_string;
2602 char_u *s2 = tv2->vval.v_string;
2603 size_t len1 = STRLEN(s1);
2604
2605 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2606 if (tv1->vval.v_string == NULL)
2607 {
2608 clear_ppconst(ppconst);
2609 return FAIL;
2610 }
2611 mch_memmove(tv1->vval.v_string, s1, len1);
2612 STRCPY(tv1->vval.v_string + len1, s2);
2613 vim_free(s1);
2614 vim_free(s2);
2615 }
2616 --ppconst->pp_used;
2617 }
2618 else
2619 {
2620 generate_ppconst(cctx, ppconst);
2621 ppconst->pp_is_const = FALSE;
2622 if (*op == '.')
2623 {
2624 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2625 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2626 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002627 if (generate_CONCAT(cctx, 2) == FAIL)
2628 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002629 }
2630 else
2631 generate_two_op(cctx, op);
2632 }
2633 }
2634
2635 return OK;
2636}
2637
2638/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002639 * expr6a >> expr6b
2640 * expr6a << expr6b
2641 *
2642 * Produces instructions:
2643 * OPNR bitwise left or right shift
2644 */
2645 static int
2646compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2647{
2648 exprtype_T type = EXPR_UNKNOWN;
2649 char_u *p;
2650 char_u *next;
2651 int len = 2;
2652 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002653 isn_T *isn;
2654
2655 // get the first variable
2656 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2657 return FAIL;
2658
2659 /*
2660 * Repeat computing, until no "+", "-" or ".." is following.
2661 */
2662 for (;;)
2663 {
2664 type = EXPR_UNKNOWN;
2665
2666 p = may_peek_next_line(cctx, *arg, &next);
2667 if (p[0] == '<' && p[1] == '<')
2668 type = EXPR_LSHIFT;
2669 else if (p[0] == '>' && p[1] == '>')
2670 type = EXPR_RSHIFT;
2671
2672 if (type == EXPR_UNKNOWN)
2673 return OK;
2674
2675 // Handle a bitwise left or right shift operator
2676 if (ppconst->pp_used == ppconst_used + 1)
2677 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002678 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002679 {
2680 // left operand should be a number
2681 emsg(_(e_bitshift_ops_must_be_number));
2682 return FAIL;
2683 }
2684 }
2685 else
2686 {
2687 type_T *t = get_type_on_stack(cctx, 0);
2688
2689 if (need_type(t, &t_number, 0, 0, cctx, FALSE, FALSE) == FAIL)
2690 {
2691 emsg(_(e_bitshift_ops_must_be_number));
2692 return FAIL;
2693 }
2694 }
2695
2696 if (next != NULL)
2697 {
2698 *arg = next_line_from_context(cctx, TRUE);
2699 p = skipwhite(*arg);
2700 }
2701
2702 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2703 {
2704 error_white_both(p, len);
2705 return FAIL;
2706 }
2707
2708 // get the second variable
2709 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2710 return FAIL;
2711
2712 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2713 return FAIL;
2714
2715 if (ppconst->pp_used == ppconst_used + 2)
2716 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002717 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2718 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2719
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002720 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002721 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2722 {
2723 // right operand should be a positive number
2724 if (tv2->v_type != VAR_NUMBER)
2725 emsg(_(e_bitshift_ops_must_be_number));
2726 else
2727 emsg(_(e_bitshift_ops_must_be_postive));
2728 return FAIL;
2729 }
2730
2731 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2732 tv1->vval.v_number = 0;
2733 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002734 tv1->vval.v_number =
2735 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002736 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002737 tv1->vval.v_number =
2738 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002739 clear_tv(tv2);
2740 --ppconst->pp_used;
2741 }
2742 else
2743 {
2744 if (need_type(get_type_on_stack(cctx, 0), &t_number, 0, 0, cctx,
2745 FALSE, FALSE) == FAIL)
2746 {
2747 emsg(_(e_bitshift_ops_must_be_number));
2748 return FAIL;
2749 }
2750
2751 generate_ppconst(cctx, ppconst);
2752
2753 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2754 if (isn == NULL)
2755 return FAIL;
2756
2757 if (isn != NULL)
2758 isn->isn_arg.op.op_type = type;
2759 }
2760 }
2761
2762 return OK;
2763}
2764
2765/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002766 * expr5a == expr5b
2767 * expr5a =~ expr5b
2768 * expr5a != expr5b
2769 * expr5a !~ expr5b
2770 * expr5a > expr5b
2771 * expr5a >= expr5b
2772 * expr5a < expr5b
2773 * expr5a <= expr5b
2774 * expr5a is expr5b
2775 * expr5a isnot expr5b
2776 *
2777 * Produces instructions:
2778 * EVAL expr5a Push result of "expr5a"
2779 * EVAL expr5b Push result of "expr5b"
2780 * COMPARE one of the compare instructions
2781 */
2782 static int
2783compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2784{
2785 exprtype_T type = EXPR_UNKNOWN;
2786 char_u *p;
2787 char_u *next;
2788 int len = 2;
2789 int type_is = FALSE;
2790 int ppconst_used = ppconst->pp_used;
2791
2792 // get the first variable
2793 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2794 return FAIL;
2795
2796 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002797
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002798 type = get_compare_type(p, &len, &type_is);
2799
2800 /*
2801 * If there is a comparative operator, use it.
2802 */
2803 if (type != EXPR_UNKNOWN)
2804 {
2805 int ic = FALSE; // Default: do not ignore case
2806
2807 if (next != NULL)
2808 {
2809 *arg = next_line_from_context(cctx, TRUE);
2810 p = skipwhite(*arg);
2811 }
2812 if (type_is && (p[len] == '?' || p[len] == '#'))
2813 {
2814 semsg(_(e_invalid_expression_str), *arg);
2815 return FAIL;
2816 }
2817 // extra question mark appended: ignore case
2818 if (p[len] == '?')
2819 {
2820 ic = TRUE;
2821 ++len;
2822 }
2823 // extra '#' appended: match case (ignored)
2824 else if (p[len] == '#')
2825 ++len;
2826 // nothing appended: match case
2827
2828 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2829 {
2830 error_white_both(p, len);
2831 return FAIL;
2832 }
2833
2834 // get the second variable
2835 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2836 return FAIL;
2837
2838 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2839 return FAIL;
2840
2841 if (ppconst->pp_used == ppconst_used + 2)
2842 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002843 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002844 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2845 int ret;
2846
2847 // Both sides are a constant, compute the result now.
2848 // First check for a valid combination of types, this is more
2849 // strict than typval_compare().
2850 if (check_compare_types(type, tv1, tv2) == FAIL)
2851 ret = FAIL;
2852 else
2853 {
2854 ret = typval_compare(tv1, tv2, type, ic);
2855 tv1->v_type = VAR_BOOL;
2856 tv1->vval.v_number = tv1->vval.v_number
2857 ? VVAL_TRUE : VVAL_FALSE;
2858 clear_tv(tv2);
2859 --ppconst->pp_used;
2860 }
2861 return ret;
2862 }
2863
2864 generate_ppconst(cctx, ppconst);
2865 return generate_COMPARE(cctx, type, ic);
2866 }
2867
2868 return OK;
2869}
2870
2871static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2872
2873/*
2874 * Compile || or &&.
2875 */
2876 static int
2877compile_and_or(
2878 char_u **arg,
2879 cctx_T *cctx,
2880 char *op,
2881 ppconst_T *ppconst,
2882 int ppconst_used UNUSED)
2883{
2884 char_u *next;
2885 char_u *p = may_peek_next_line(cctx, *arg, &next);
2886 int opchar = *op;
2887
2888 if (p[0] == opchar && p[1] == opchar)
2889 {
2890 garray_T *instr = &cctx->ctx_instr;
2891 garray_T end_ga;
2892 int save_skip = cctx->ctx_skip;
2893
2894 /*
2895 * Repeat until there is no following "||" or "&&"
2896 */
2897 ga_init2(&end_ga, sizeof(int), 10);
2898 while (p[0] == opchar && p[1] == opchar)
2899 {
2900 long start_lnum = SOURCING_LNUM;
2901 long save_sourcing_lnum;
2902 int start_ctx_lnum = cctx->ctx_lnum;
2903 int save_lnum;
2904 int const_used;
2905 int status;
2906 jumpwhen_T jump_when = opchar == '|'
2907 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2908
2909 if (next != NULL)
2910 {
2911 *arg = next_line_from_context(cctx, TRUE);
2912 p = skipwhite(*arg);
2913 }
2914
2915 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2916 {
2917 semsg(_(e_white_space_required_before_and_after_str_at_str),
2918 op, p);
2919 ga_clear(&end_ga);
2920 return FAIL;
2921 }
2922
2923 save_sourcing_lnum = SOURCING_LNUM;
2924 SOURCING_LNUM = start_lnum;
2925 save_lnum = cctx->ctx_lnum;
2926 cctx->ctx_lnum = start_ctx_lnum;
2927
2928 status = check_ppconst_bool(ppconst);
2929 if (status != FAIL)
2930 {
2931 // Use the last ppconst if possible.
2932 if (ppconst->pp_used > 0)
2933 {
2934 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2935 int is_true = tv2bool(tv);
2936
2937 if ((is_true && opchar == '|')
2938 || (!is_true && opchar == '&'))
2939 {
2940 // For "false && expr" and "true || expr" the "expr"
2941 // does not need to be evaluated.
2942 cctx->ctx_skip = SKIP_YES;
2943 clear_tv(tv);
2944 tv->v_type = VAR_BOOL;
2945 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2946 }
2947 else
2948 {
2949 // For "true && expr" and "false || expr" only "expr"
2950 // needs to be evaluated.
2951 --ppconst->pp_used;
2952 jump_when = JUMP_NEVER;
2953 }
2954 }
2955 else
2956 {
2957 // Every part must evaluate to a bool.
2958 status = bool_on_stack(cctx);
2959 }
2960 }
2961 if (status != FAIL)
2962 status = ga_grow(&end_ga, 1);
2963 cctx->ctx_lnum = save_lnum;
2964 if (status == FAIL)
2965 {
2966 ga_clear(&end_ga);
2967 return FAIL;
2968 }
2969
2970 if (jump_when != JUMP_NEVER)
2971 {
2972 if (cctx->ctx_skip != SKIP_YES)
2973 {
2974 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2975 ++end_ga.ga_len;
2976 }
2977 generate_JUMP(cctx, jump_when, 0);
2978 }
2979
2980 // eval the next expression
2981 SOURCING_LNUM = save_sourcing_lnum;
2982 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2983 {
2984 ga_clear(&end_ga);
2985 return FAIL;
2986 }
2987
2988 const_used = ppconst->pp_used;
2989 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
2990 : compile_expr4(arg, cctx, ppconst)) == FAIL)
2991 {
2992 ga_clear(&end_ga);
2993 return FAIL;
2994 }
2995
2996 // "0 || 1" results in true, "1 && 0" results in false.
2997 if (ppconst->pp_used == const_used + 1)
2998 {
2999 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3000
3001 if (tv->v_type == VAR_NUMBER
3002 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3003 {
3004 tv->vval.v_number = tv->vval.v_number == 1
3005 ? VVAL_TRUE : VVAL_FALSE;
3006 tv->v_type = VAR_BOOL;
3007 }
3008 }
3009
3010 p = may_peek_next_line(cctx, *arg, &next);
3011 }
3012
3013 if (check_ppconst_bool(ppconst) == FAIL)
3014 {
3015 ga_clear(&end_ga);
3016 return FAIL;
3017 }
3018
3019 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3020 // Every part must evaluate to a bool.
3021 if (bool_on_stack(cctx) == FAIL)
3022 {
3023 ga_clear(&end_ga);
3024 return FAIL;
3025 }
3026
3027 if (end_ga.ga_len > 0)
3028 {
3029 // Fill in the end label in all jumps.
3030 generate_ppconst(cctx, ppconst);
3031 while (end_ga.ga_len > 0)
3032 {
3033 isn_T *isn;
3034
3035 --end_ga.ga_len;
3036 isn = ((isn_T *)instr->ga_data)
3037 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3038 isn->isn_arg.jump.jump_where = instr->ga_len;
3039 }
3040 }
3041 ga_clear(&end_ga);
3042
3043 cctx->ctx_skip = save_skip;
3044 }
3045
3046 return OK;
3047}
3048
3049/*
3050 * expr4a && expr4a && expr4a logical AND
3051 *
3052 * Produces instructions:
3053 * EVAL expr4a Push result of "expr4a"
3054 * COND2BOOL convert to bool if needed
3055 * JUMP_IF_COND_FALSE end
3056 * EVAL expr4b Push result of "expr4b"
3057 * JUMP_IF_COND_FALSE end
3058 * EVAL expr4c Push result of "expr4c"
3059 * end:
3060 */
3061 static int
3062compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3063{
3064 int ppconst_used = ppconst->pp_used;
3065
3066 // get the first variable
3067 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3068 return FAIL;
3069
3070 // || and && work almost the same
3071 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3072}
3073
3074/*
3075 * expr3a || expr3b || expr3c logical OR
3076 *
3077 * Produces instructions:
3078 * EVAL expr3a Push result of "expr3a"
3079 * COND2BOOL convert to bool if needed
3080 * JUMP_IF_COND_TRUE end
3081 * EVAL expr3b Push result of "expr3b"
3082 * JUMP_IF_COND_TRUE end
3083 * EVAL expr3c Push result of "expr3c"
3084 * end:
3085 */
3086 static int
3087compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3088{
3089 int ppconst_used = ppconst->pp_used;
3090
3091 // eval the first expression
3092 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3093 return FAIL;
3094
3095 // || and && work almost the same
3096 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3097}
3098
3099/*
3100 * Toplevel expression: expr2 ? expr1a : expr1b
3101 * Produces instructions:
3102 * EVAL expr2 Push result of "expr2"
3103 * JUMP_IF_FALSE alt jump if false
3104 * EVAL expr1a
3105 * JUMP_ALWAYS end
3106 * alt: EVAL expr1b
3107 * end:
3108 *
3109 * Toplevel expression: expr2 ?? expr1
3110 * Produces instructions:
3111 * EVAL expr2 Push result of "expr2"
3112 * JUMP_AND_KEEP_IF_TRUE end jump if true
3113 * EVAL expr1
3114 * end:
3115 */
3116 int
3117compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3118{
3119 char_u *p;
3120 int ppconst_used = ppconst->pp_used;
3121 char_u *next;
3122
3123 // Ignore all kinds of errors when not producing code.
3124 if (cctx->ctx_skip == SKIP_YES)
3125 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003126 int prev_did_emsg = did_emsg;
3127
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003128 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003129 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003130 }
3131
3132 // Evaluate the first expression.
3133 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3134 return FAIL;
3135
3136 p = may_peek_next_line(cctx, *arg, &next);
3137 if (*p == '?')
3138 {
3139 int op_falsy = p[1] == '?';
3140 garray_T *instr = &cctx->ctx_instr;
3141 garray_T *stack = &cctx->ctx_type_stack;
3142 int alt_idx = instr->ga_len;
3143 int end_idx = 0;
3144 isn_T *isn;
3145 type_T *type1 = NULL;
3146 int has_const_expr = FALSE;
3147 int const_value = FALSE;
3148 int save_skip = cctx->ctx_skip;
3149
3150 if (next != NULL)
3151 {
3152 *arg = next_line_from_context(cctx, TRUE);
3153 p = skipwhite(*arg);
3154 }
3155
3156 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3157 {
3158 semsg(_(e_white_space_required_before_and_after_str_at_str),
3159 op_falsy ? "??" : "?", p);
3160 return FAIL;
3161 }
3162
3163 if (ppconst->pp_used == ppconst_used + 1)
3164 {
3165 // the condition is a constant, we know whether the ? or the :
3166 // expression is to be evaluated.
3167 has_const_expr = TRUE;
3168 if (op_falsy)
3169 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3170 else
3171 {
3172 int error = FALSE;
3173
3174 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3175 &error);
3176 if (error)
3177 return FAIL;
3178 }
3179 cctx->ctx_skip = save_skip == SKIP_YES ||
3180 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3181
3182 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3183 // "left ?? right" and "left" is truthy: produce "left"
3184 generate_ppconst(cctx, ppconst);
3185 else
3186 {
3187 clear_tv(&ppconst->pp_tv[ppconst_used]);
3188 --ppconst->pp_used;
3189 }
3190 }
3191 else
3192 {
3193 generate_ppconst(cctx, ppconst);
3194 if (op_falsy)
3195 end_idx = instr->ga_len;
3196 generate_JUMP(cctx, op_falsy
3197 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3198 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003199 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003200 }
3201
3202 // evaluate the second expression; any type is accepted
3203 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3204 return FAIL;
3205 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3206 return FAIL;
3207
3208 if (!has_const_expr)
3209 {
3210 generate_ppconst(cctx, ppconst);
3211
3212 if (!op_falsy)
3213 {
3214 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003215 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003216 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003217
3218 end_idx = instr->ga_len;
3219 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3220
3221 // jump here from JUMP_IF_FALSE
3222 isn = ((isn_T *)instr->ga_data) + alt_idx;
3223 isn->isn_arg.jump.jump_where = instr->ga_len;
3224 }
3225 }
3226
3227 if (!op_falsy)
3228 {
3229 // Check for the ":".
3230 p = may_peek_next_line(cctx, *arg, &next);
3231 if (*p != ':')
3232 {
3233 emsg(_(e_missing_colon_after_questionmark));
3234 return FAIL;
3235 }
3236 if (next != NULL)
3237 {
3238 *arg = next_line_from_context(cctx, TRUE);
3239 p = skipwhite(*arg);
3240 }
3241
3242 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3243 {
3244 semsg(_(e_white_space_required_before_and_after_str_at_str),
3245 ":", p);
3246 return FAIL;
3247 }
3248
3249 // evaluate the third expression
3250 if (has_const_expr)
3251 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3252 ? SKIP_YES : SKIP_NOT;
3253 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3254 return FAIL;
3255 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3256 return FAIL;
3257 }
3258
3259 if (!has_const_expr)
3260 {
3261 type_T **typep;
3262
3263 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003264 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003265
3266 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003267 typep = &((((type2_T *)stack->ga_data)
3268 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003269 common_type(type1, *typep, typep, cctx->ctx_type_list);
3270
3271 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3272 isn = ((isn_T *)instr->ga_data) + end_idx;
3273 isn->isn_arg.jump.jump_where = instr->ga_len;
3274 }
3275
3276 cctx->ctx_skip = save_skip;
3277 }
3278 return OK;
3279}
3280
3281/*
3282 * Toplevel expression.
3283 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3284 * Returns OK or FAIL.
3285 */
3286 int
3287compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3288{
3289 ppconst_T ppconst;
3290
3291 CLEAR_FIELD(ppconst);
3292 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3293 {
3294 clear_ppconst(&ppconst);
3295 return FAIL;
3296 }
3297 if (is_const != NULL)
3298 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3299 if (generate_ppconst(cctx, &ppconst) == FAIL)
3300 return FAIL;
3301 return OK;
3302}
3303
3304/*
3305 * Toplevel expression.
3306 */
3307 int
3308compile_expr0(char_u **arg, cctx_T *cctx)
3309{
3310 return compile_expr0_ext(arg, cctx, NULL);
3311}
3312
3313
3314#endif // defined(FEAT_EVAL)