blob: 1fbe05b0727d97c55ba02af64aaba43cd26c89a6 [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
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001791 // If a following line starts with "->{", "->(" or "->X" advance to
1792 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001793 // Also if a following line starts with ".x".
1794 if (next != NULL &&
1795 ((next[0] == '-' && next[1] == '>'
1796 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001797 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001798 || ASCII_ISALPHA(*skipwhite(next + 2))))
1799 || (next[0] == '.' && eval_isdictc(next[1]))))
1800 {
1801 next = next_line_from_context(cctx, TRUE);
1802 if (next == NULL)
1803 return FAIL;
1804 *arg = next;
1805 p = skipwhite(*arg);
1806 }
1807 }
1808
1809 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1810 // is not a function call.
1811 if (**arg == '(')
1812 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001813 type_T *type;
1814 int argcount = 0;
1815
1816 if (generate_ppconst(cctx, ppconst) == FAIL)
1817 return FAIL;
1818 ppconst->pp_is_const = FALSE;
1819
1820 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001821 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001822
1823 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001824 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001825 return FAIL;
1826 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1827 return FAIL;
1828 if (keeping_dict)
1829 {
1830 keeping_dict = FALSE;
1831 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1832 return FAIL;
1833 }
1834 }
1835 else if (*p == '-' && p[1] == '>')
1836 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001837 char_u *pstart = p;
1838 int alt;
1839 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001840
Bram Moolenaarc7349932022-01-16 20:59:39 +00001841 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001842 if (generate_ppconst(cctx, ppconst) == FAIL)
1843 return FAIL;
1844 ppconst->pp_is_const = FALSE;
1845
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001846 // Apply the '!', '-' and '+' first:
1847 // -1.0->func() works like (-1.0)->func()
1848 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1849 return FAIL;
1850
1851 p += 2;
1852 *arg = skipwhite(p);
1853 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001854
1855 // Three alternatives handled here:
1856 // 1. "base->name(" only a name, use compile_call()
1857 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1858 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001859 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001860 alt = 2;
1861 else
1862 {
1863 // alternative 1 or 3
1864 p = *arg;
1865 if (!eval_isnamec1(*p))
1866 {
1867 semsg(_(e_trailing_characters_str), pstart);
1868 return FAIL;
1869 }
1870 if (ASCII_ISALPHA(*p) && p[1] == ':')
1871 p += 2;
1872 for ( ; eval_isnamec(*p); ++p)
1873 ;
1874 if (*p == '(')
1875 {
1876 // alternative 1
1877 alt = 1;
1878 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1879 return FAIL;
1880 }
1881 else
1882 {
1883 // Must be alternative 3, find the "(". Only works within
1884 // one line.
1885 alt = 3;
1886 paren = vim_strchr(p, '(');
1887 if (paren == NULL)
1888 {
1889 semsg(_(e_missing_parenthesis_str), *arg);
1890 return FAIL;
1891 }
1892 }
1893 }
1894
1895 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001896 {
1897 int argcount = 1;
1898 garray_T *stack = &cctx->ctx_type_stack;
1899 int type_idx_start = stack->ga_len;
1900 type_T *type;
1901 int expr_isn_start = cctx->ctx_instr.ga_len;
1902 int expr_isn_end;
1903 int arg_isn_count;
1904
Bram Moolenaarc7349932022-01-16 20:59:39 +00001905 if (alt == 2)
1906 {
1907 // Funcref call: list->(Refs[2])(arg)
1908 // or lambda: list->((arg) => expr)(arg)
1909 //
1910 // Fist compile the function expression.
1911 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1912 return FAIL;
1913 }
1914 else
1915 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001916 int fail;
1917 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001918 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001919
Bram Moolenaarc7349932022-01-16 20:59:39 +00001920 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001921
1922 // instead of using LOADG for "import.Func" use PUSHFUNC
1923 ++paren_follows_after_expr;
1924
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001925 // do not look in the next line
1926 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001927
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001928 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001929 || *skipwhite(*arg) != NUL;
1930 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001931 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001932 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001933
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001934 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001935 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001936 if (did_emsg == prev_did_emsg)
1937 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001938 return FAIL;
1939 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001940 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001941
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001942 // Compile the arguments.
1943 if (**arg != '(')
1944 {
1945 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001946 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001947 else
1948 semsg(_(e_missing_parenthesis_str), *arg);
1949 return FAIL;
1950 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001951
1952 // Remember the next instruction index, where the instructions
1953 // for arguments are being written.
1954 expr_isn_end = cctx->ctx_instr.ga_len;
1955
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001956 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001957 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
1958 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001959 return FAIL;
1960
1961 // Move the instructions for the arguments to before the
1962 // instructions of the expression and move the type of the
1963 // expression after the argument types. This is what ISN_PCALL
1964 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001965 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1966 if (arg_isn_count > 0)
1967 {
1968 int expr_isn_count = expr_isn_end - expr_isn_start;
1969 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001970 type_T *decl_type;
1971 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001972
1973 if (isn == NULL)
1974 return FAIL;
1975 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1976 + expr_isn_start,
1977 sizeof(isn_T) * expr_isn_count);
1978 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1979 + expr_isn_start,
1980 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1981 sizeof(isn_T) * arg_isn_count);
1982 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1983 + expr_isn_start + arg_isn_count,
1984 isn, sizeof(isn_T) * expr_isn_count);
1985 vim_free(isn);
1986
Bram Moolenaar078a4612022-01-04 15:17:03 +00001987 typep = ((type2_T *)stack->ga_data) + type_idx_start;
1988 type = typep->type_curr;
1989 decl_type = typep->type_decl;
1990 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
1991 ((type2_T *)stack->ga_data) + type_idx_start + 1,
1992 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001993 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00001994 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
1995 typep->type_curr = type;
1996 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001997 }
1998
Bram Moolenaar078a4612022-01-04 15:17:03 +00001999 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002000 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2001 return FAIL;
2002 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002003
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002004 if (keeping_dict)
2005 {
2006 keeping_dict = FALSE;
2007 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2008 return FAIL;
2009 }
2010 }
2011 else if (**arg == '[')
2012 {
2013 int is_slice = FALSE;
2014
2015 // list index: list[123]
2016 // dict member: dict[key]
2017 // string index: text[123]
2018 // blob index: blob[123]
2019 if (generate_ppconst(cctx, ppconst) == FAIL)
2020 return FAIL;
2021 ppconst->pp_is_const = FALSE;
2022
2023 ++p;
2024 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2025 return FAIL;
2026 if (**arg == ':')
2027 {
2028 // missing first index is equal to zero
2029 generate_PUSHNR(cctx, 0);
2030 }
2031 else
2032 {
2033 if (compile_expr0(arg, cctx) == FAIL)
2034 return FAIL;
2035 if (**arg == ':')
2036 {
2037 semsg(_(e_white_space_required_before_and_after_str_at_str),
2038 ":", *arg);
2039 return FAIL;
2040 }
2041 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2042 return FAIL;
2043 *arg = skipwhite(*arg);
2044 }
2045 if (**arg == ':')
2046 {
2047 is_slice = TRUE;
2048 ++*arg;
2049 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2050 {
2051 semsg(_(e_white_space_required_before_and_after_str_at_str),
2052 ":", *arg);
2053 return FAIL;
2054 }
2055 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2056 return FAIL;
2057 if (**arg == ']')
2058 // missing second index is equal to end of string
2059 generate_PUSHNR(cctx, -1);
2060 else
2061 {
2062 if (compile_expr0(arg, cctx) == FAIL)
2063 return FAIL;
2064 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2065 return FAIL;
2066 *arg = skipwhite(*arg);
2067 }
2068 }
2069
2070 if (**arg != ']')
2071 {
2072 emsg(_(e_missing_closing_square_brace));
2073 return FAIL;
2074 }
2075 *arg = *arg + 1;
2076
2077 if (keeping_dict)
2078 {
2079 keeping_dict = FALSE;
2080 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2081 return FAIL;
2082 }
2083 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2084 return FAIL;
2085 }
2086 else if (*p == '.' && p[1] != '.')
2087 {
2088 // dictionary member: dict.name
2089 if (generate_ppconst(cctx, ppconst) == FAIL)
2090 return FAIL;
2091 ppconst->pp_is_const = FALSE;
2092
2093 *arg = p + 1;
2094 if (IS_WHITE_OR_NUL(**arg))
2095 {
2096 emsg(_(e_missing_name_after_dot));
2097 return FAIL;
2098 }
2099 p = *arg;
2100 if (eval_isdictc(*p))
2101 while (eval_isnamec(*p))
2102 MB_PTR_ADV(p);
2103 if (p == *arg)
2104 {
2105 semsg(_(e_syntax_error_at_str), *arg);
2106 return FAIL;
2107 }
2108 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2109 return FAIL;
2110 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2111 return FAIL;
2112 keeping_dict = TRUE;
2113 *arg = p;
2114 }
2115 else
2116 break;
2117 }
2118
2119 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2120 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002121 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2122 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002123 return FAIL;
2124
2125 return OK;
2126}
2127
2128/*
2129 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2130 * "arg" is advanced until after the expression, skipping white space.
2131 *
2132 * If the value is a constant "ppconst->pp_used" will be non-zero.
2133 * Before instructions are generated, any values in "ppconst" will generated.
2134 *
2135 * This is the compiling equivalent of eval1(), eval2(), etc.
2136 */
2137
2138/*
2139 * number number constant
2140 * 0zFFFFFFFF Blob constant
2141 * "string" string constant
2142 * 'string' literal string constant
2143 * &option-name option value
2144 * @r register contents
2145 * identifier variable value
2146 * function() function call
2147 * $VAR environment variable
2148 * (expression) nested expression
2149 * [expr, expr] List
2150 * {key: val, [key]: val} Dictionary
2151 *
2152 * Also handle:
2153 * ! in front logical NOT
2154 * - in front unary minus
2155 * + in front unary plus (ignored)
2156 * trailing (arg) funcref/partial call
2157 * trailing [] subscript in String or List
2158 * trailing .name entry in Dictionary
2159 * trailing ->name() method call
2160 */
2161 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002162compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002163 char_u **arg,
2164 cctx_T *cctx,
2165 ppconst_T *ppconst)
2166{
2167 char_u *start_leader, *end_leader;
2168 int ret = OK;
2169 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2170 int used_before = ppconst->pp_used;
2171
2172 ppconst->pp_is_const = FALSE;
2173
2174 /*
2175 * Skip '!', '-' and '+' characters. They are handled later.
2176 */
2177 start_leader = *arg;
2178 if (eval_leader(arg, TRUE) == FAIL)
2179 return FAIL;
2180 end_leader = *arg;
2181
2182 rettv->v_type = VAR_UNKNOWN;
2183 switch (**arg)
2184 {
2185 /*
2186 * Number constant.
2187 */
2188 case '0': // also for blob starting with 0z
2189 case '1':
2190 case '2':
2191 case '3':
2192 case '4':
2193 case '5':
2194 case '6':
2195 case '7':
2196 case '8':
2197 case '9':
2198 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2199 return FAIL;
2200 // Apply "-" and "+" just before the number now, right to
2201 // left. Matters especially when "->" follows. Stops at
2202 // '!'.
2203 if (apply_leader(rettv, TRUE,
2204 start_leader, &end_leader) == FAIL)
2205 {
2206 clear_tv(rettv);
2207 return FAIL;
2208 }
2209 break;
2210
2211 /*
2212 * String constant: "string".
2213 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002214 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002215 return FAIL;
2216 break;
2217
2218 /*
2219 * Literal string constant: 'str''ing'.
2220 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002221 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002222 return FAIL;
2223 break;
2224
2225 /*
2226 * Constant Vim variable.
2227 */
2228 case 'v': get_vim_constant(arg, rettv);
2229 ret = NOTDONE;
2230 break;
2231
2232 /*
2233 * "true" constant
2234 */
2235 case 't': if (STRNCMP(*arg, "true", 4) == 0
2236 && !eval_isnamec((*arg)[4]))
2237 {
2238 *arg += 4;
2239 rettv->v_type = VAR_BOOL;
2240 rettv->vval.v_number = VVAL_TRUE;
2241 }
2242 else
2243 ret = NOTDONE;
2244 break;
2245
2246 /*
2247 * "false" constant
2248 */
2249 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2250 && !eval_isnamec((*arg)[5]))
2251 {
2252 *arg += 5;
2253 rettv->v_type = VAR_BOOL;
2254 rettv->vval.v_number = VVAL_FALSE;
2255 }
2256 else
2257 ret = NOTDONE;
2258 break;
2259
2260 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002261 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002262 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002263 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002264 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002265 char_u *p = *arg + 4;
2266 int len;
2267
2268 for (len = 0; eval_isnamec(p[len]); ++len)
2269 ;
2270 ret = handle_predefined(*arg, len + 4, rettv);
2271 if (ret == FAIL)
2272 ret = NOTDONE;
2273 else
2274 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002275 }
2276 else
2277 ret = NOTDONE;
2278 break;
2279
2280 /*
2281 * List: [expr, expr]
2282 */
2283 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2284 return FAIL;
2285 ret = compile_list(arg, cctx, ppconst);
2286 break;
2287
2288 /*
2289 * Dictionary: {'key': val, 'key': val}
2290 */
2291 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2292 return FAIL;
2293 ret = compile_dict(arg, cctx, ppconst);
2294 break;
2295
2296 /*
2297 * Option value: &name
2298 */
2299 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2300 return FAIL;
2301 ret = compile_get_option(arg, cctx);
2302 break;
2303
2304 /*
2305 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002306 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002307 */
2308 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2309 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002310 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2311 ret = compile_interp_string(arg, cctx);
2312 else
2313 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002314 break;
2315
2316 /*
2317 * Register contents: @r.
2318 */
2319 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2320 return FAIL;
2321 ret = compile_get_register(arg, cctx);
2322 break;
2323 /*
2324 * nested expression: (expression).
2325 * lambda: (arg, arg) => expr
2326 * funcref: (arg, arg) => { statement }
2327 */
2328 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2329 ret = compile_lambda(arg, cctx);
2330 if (ret == NOTDONE)
2331 ret = compile_parenthesis(arg, cctx, ppconst);
2332 break;
2333
2334 default: ret = NOTDONE;
2335 break;
2336 }
2337 if (ret == FAIL)
2338 return FAIL;
2339
2340 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2341 {
2342 if (cctx->ctx_skip == SKIP_YES)
2343 clear_tv(rettv);
2344 else
2345 // A constant expression can possibly be handled compile time,
2346 // return the value instead of generating code.
2347 ++ppconst->pp_used;
2348 }
2349 else if (ret == NOTDONE)
2350 {
2351 char_u *p;
2352 int r;
2353
2354 if (!eval_isnamec1(**arg))
2355 {
2356 if (!vim9_bad_comment(*arg))
2357 {
2358 if (ends_excmd(*skipwhite(*arg)))
2359 semsg(_(e_empty_expression_str), *arg);
2360 else
2361 semsg(_(e_name_expected_str), *arg);
2362 }
2363 return FAIL;
2364 }
2365
2366 // "name" or "name()"
2367 p = to_name_end(*arg, TRUE);
2368 if (p - *arg == (size_t)1 && **arg == '_')
2369 {
2370 emsg(_(e_cannot_use_underscore_here));
2371 return FAIL;
2372 }
2373
2374 if (*p == '(')
2375 {
2376 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2377 }
2378 else
2379 {
2380 if (cctx->ctx_skip != SKIP_YES
2381 && generate_ppconst(cctx, ppconst) == FAIL)
2382 return FAIL;
2383 r = compile_load(arg, p, cctx, TRUE, TRUE);
2384 }
2385 if (r == FAIL)
2386 return FAIL;
2387 }
2388
2389 // Handle following "[]", ".member", etc.
2390 // Then deal with prefixed '-', '+' and '!', if not done already.
2391 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2392 ppconst) == FAIL)
2393 return FAIL;
2394 if (ppconst->pp_used > 0)
2395 {
2396 // apply the '!', '-' and '+' before the constant
2397 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2398 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2399 return FAIL;
2400 return OK;
2401 }
2402 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2403 return FAIL;
2404 return OK;
2405}
2406
2407/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002408 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002409 */
2410 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002411compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002412{
2413 type_T *want_type = NULL;
2414
2415 // Recognize <type>
2416 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2417 {
2418 ++*arg;
2419 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2420 if (want_type == NULL)
2421 return FAIL;
2422
2423 if (**arg != '>')
2424 {
2425 if (*skipwhite(*arg) == '>')
2426 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2427 else
2428 emsg(_(e_missing_gt));
2429 return FAIL;
2430 }
2431 ++*arg;
2432 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2433 return FAIL;
2434 }
2435
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002436 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002437 return FAIL;
2438
2439 if (want_type != NULL)
2440 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002441 type_T *actual;
2442 where_T where = WHERE_INIT;
2443
2444 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002445 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002446 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002447 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002448 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2449 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002450 return FAIL;
2451 }
2452 }
2453
2454 return OK;
2455}
2456
2457/*
2458 * * number multiplication
2459 * / number division
2460 * % number modulo
2461 */
2462 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002463compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002464{
2465 char_u *op;
2466 char_u *next;
2467 int ppconst_used = ppconst->pp_used;
2468
2469 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002470 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002471 return FAIL;
2472
2473 /*
2474 * Repeat computing, until no "*", "/" or "%" is following.
2475 */
2476 for (;;)
2477 {
2478 op = may_peek_next_line(cctx, *arg, &next);
2479 if (*op != '*' && *op != '/' && *op != '%')
2480 break;
2481 if (next != NULL)
2482 {
2483 *arg = next_line_from_context(cctx, TRUE);
2484 op = skipwhite(*arg);
2485 }
2486
2487 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2488 {
2489 error_white_both(op, 1);
2490 return FAIL;
2491 }
2492 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2493 return FAIL;
2494
2495 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002496 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002497 return FAIL;
2498
2499 if (ppconst->pp_used == ppconst_used + 2
2500 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2501 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2502 {
2503 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2504 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2505 varnumber_T res = 0;
2506 int failed = FALSE;
2507
2508 // both are numbers: compute the result
2509 switch (*op)
2510 {
2511 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2512 break;
2513 case '/': res = num_divide(tv1->vval.v_number,
2514 tv2->vval.v_number, &failed);
2515 break;
2516 case '%': res = num_modulus(tv1->vval.v_number,
2517 tv2->vval.v_number, &failed);
2518 break;
2519 }
2520 if (failed)
2521 return FAIL;
2522 tv1->vval.v_number = res;
2523 --ppconst->pp_used;
2524 }
2525 else
2526 {
2527 generate_ppconst(cctx, ppconst);
2528 generate_two_op(cctx, op);
2529 }
2530 }
2531
2532 return OK;
2533}
2534
2535/*
2536 * + number addition or list/blobl concatenation
2537 * - number subtraction
2538 * .. string concatenation
2539 */
2540 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002541compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002542{
2543 char_u *op;
2544 char_u *next;
2545 int oplen;
2546 int ppconst_used = ppconst->pp_used;
2547
2548 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002549 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002550 return FAIL;
2551
2552 /*
2553 * Repeat computing, until no "+", "-" or ".." is following.
2554 */
2555 for (;;)
2556 {
2557 op = may_peek_next_line(cctx, *arg, &next);
2558 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2559 break;
2560 if (op[0] == op[1] && *op != '.' && next)
2561 // Finding "++" or "--" on the next line is a separate command.
2562 // But ".." is concatenation.
2563 break;
2564 oplen = (*op == '.' ? 2 : 1);
2565 if (next != NULL)
2566 {
2567 *arg = next_line_from_context(cctx, TRUE);
2568 op = skipwhite(*arg);
2569 }
2570
2571 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2572 {
2573 error_white_both(op, oplen);
2574 return FAIL;
2575 }
2576
2577 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2578 return FAIL;
2579
2580 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002581 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002582 return FAIL;
2583
2584 if (ppconst->pp_used == ppconst_used + 2
2585 && (*op == '.'
2586 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2587 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2588 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2589 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2590 {
2591 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2592 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2593
2594 // concat/subtract/add constant numbers
2595 if (*op == '+')
2596 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2597 else if (*op == '-')
2598 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2599 else
2600 {
2601 // concatenate constant strings
2602 char_u *s1 = tv1->vval.v_string;
2603 char_u *s2 = tv2->vval.v_string;
2604 size_t len1 = STRLEN(s1);
2605
2606 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2607 if (tv1->vval.v_string == NULL)
2608 {
2609 clear_ppconst(ppconst);
2610 return FAIL;
2611 }
2612 mch_memmove(tv1->vval.v_string, s1, len1);
2613 STRCPY(tv1->vval.v_string + len1, s2);
2614 vim_free(s1);
2615 vim_free(s2);
2616 }
2617 --ppconst->pp_used;
2618 }
2619 else
2620 {
2621 generate_ppconst(cctx, ppconst);
2622 ppconst->pp_is_const = FALSE;
2623 if (*op == '.')
2624 {
2625 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2626 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2627 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002628 if (generate_CONCAT(cctx, 2) == FAIL)
2629 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002630 }
2631 else
2632 generate_two_op(cctx, op);
2633 }
2634 }
2635
2636 return OK;
2637}
2638
2639/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002640 * expr6a >> expr6b
2641 * expr6a << expr6b
2642 *
2643 * Produces instructions:
2644 * OPNR bitwise left or right shift
2645 */
2646 static int
2647compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2648{
2649 exprtype_T type = EXPR_UNKNOWN;
2650 char_u *p;
2651 char_u *next;
2652 int len = 2;
2653 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002654 isn_T *isn;
2655
2656 // get the first variable
2657 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2658 return FAIL;
2659
2660 /*
2661 * Repeat computing, until no "+", "-" or ".." is following.
2662 */
2663 for (;;)
2664 {
2665 type = EXPR_UNKNOWN;
2666
2667 p = may_peek_next_line(cctx, *arg, &next);
2668 if (p[0] == '<' && p[1] == '<')
2669 type = EXPR_LSHIFT;
2670 else if (p[0] == '>' && p[1] == '>')
2671 type = EXPR_RSHIFT;
2672
2673 if (type == EXPR_UNKNOWN)
2674 return OK;
2675
2676 // Handle a bitwise left or right shift operator
2677 if (ppconst->pp_used == ppconst_used + 1)
2678 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002679 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002680 {
2681 // left operand should be a number
2682 emsg(_(e_bitshift_ops_must_be_number));
2683 return FAIL;
2684 }
2685 }
2686 else
2687 {
2688 type_T *t = get_type_on_stack(cctx, 0);
2689
2690 if (need_type(t, &t_number, 0, 0, cctx, FALSE, FALSE) == FAIL)
2691 {
2692 emsg(_(e_bitshift_ops_must_be_number));
2693 return FAIL;
2694 }
2695 }
2696
2697 if (next != NULL)
2698 {
2699 *arg = next_line_from_context(cctx, TRUE);
2700 p = skipwhite(*arg);
2701 }
2702
2703 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2704 {
2705 error_white_both(p, len);
2706 return FAIL;
2707 }
2708
2709 // get the second variable
2710 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2711 return FAIL;
2712
2713 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2714 return FAIL;
2715
2716 if (ppconst->pp_used == ppconst_used + 2)
2717 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002718 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2719 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2720
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002721 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002722 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2723 {
2724 // right operand should be a positive number
2725 if (tv2->v_type != VAR_NUMBER)
2726 emsg(_(e_bitshift_ops_must_be_number));
2727 else
2728 emsg(_(e_bitshift_ops_must_be_postive));
2729 return FAIL;
2730 }
2731
2732 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2733 tv1->vval.v_number = 0;
2734 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002735 tv1->vval.v_number =
2736 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002737 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002738 tv1->vval.v_number =
2739 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002740 clear_tv(tv2);
2741 --ppconst->pp_used;
2742 }
2743 else
2744 {
2745 if (need_type(get_type_on_stack(cctx, 0), &t_number, 0, 0, cctx,
2746 FALSE, FALSE) == FAIL)
2747 {
2748 emsg(_(e_bitshift_ops_must_be_number));
2749 return FAIL;
2750 }
2751
2752 generate_ppconst(cctx, ppconst);
2753
2754 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2755 if (isn == NULL)
2756 return FAIL;
2757
2758 if (isn != NULL)
2759 isn->isn_arg.op.op_type = type;
2760 }
2761 }
2762
2763 return OK;
2764}
2765
2766/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002767 * expr5a == expr5b
2768 * expr5a =~ expr5b
2769 * expr5a != expr5b
2770 * expr5a !~ expr5b
2771 * expr5a > expr5b
2772 * expr5a >= expr5b
2773 * expr5a < expr5b
2774 * expr5a <= expr5b
2775 * expr5a is expr5b
2776 * expr5a isnot expr5b
2777 *
2778 * Produces instructions:
2779 * EVAL expr5a Push result of "expr5a"
2780 * EVAL expr5b Push result of "expr5b"
2781 * COMPARE one of the compare instructions
2782 */
2783 static int
2784compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2785{
2786 exprtype_T type = EXPR_UNKNOWN;
2787 char_u *p;
2788 char_u *next;
2789 int len = 2;
2790 int type_is = FALSE;
2791 int ppconst_used = ppconst->pp_used;
2792
2793 // get the first variable
2794 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2795 return FAIL;
2796
2797 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002798
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002799 type = get_compare_type(p, &len, &type_is);
2800
2801 /*
2802 * If there is a comparative operator, use it.
2803 */
2804 if (type != EXPR_UNKNOWN)
2805 {
2806 int ic = FALSE; // Default: do not ignore case
2807
2808 if (next != NULL)
2809 {
2810 *arg = next_line_from_context(cctx, TRUE);
2811 p = skipwhite(*arg);
2812 }
2813 if (type_is && (p[len] == '?' || p[len] == '#'))
2814 {
2815 semsg(_(e_invalid_expression_str), *arg);
2816 return FAIL;
2817 }
2818 // extra question mark appended: ignore case
2819 if (p[len] == '?')
2820 {
2821 ic = TRUE;
2822 ++len;
2823 }
2824 // extra '#' appended: match case (ignored)
2825 else if (p[len] == '#')
2826 ++len;
2827 // nothing appended: match case
2828
2829 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2830 {
2831 error_white_both(p, len);
2832 return FAIL;
2833 }
2834
2835 // get the second variable
2836 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2837 return FAIL;
2838
2839 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2840 return FAIL;
2841
2842 if (ppconst->pp_used == ppconst_used + 2)
2843 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002844 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002845 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2846 int ret;
2847
2848 // Both sides are a constant, compute the result now.
2849 // First check for a valid combination of types, this is more
2850 // strict than typval_compare().
2851 if (check_compare_types(type, tv1, tv2) == FAIL)
2852 ret = FAIL;
2853 else
2854 {
2855 ret = typval_compare(tv1, tv2, type, ic);
2856 tv1->v_type = VAR_BOOL;
2857 tv1->vval.v_number = tv1->vval.v_number
2858 ? VVAL_TRUE : VVAL_FALSE;
2859 clear_tv(tv2);
2860 --ppconst->pp_used;
2861 }
2862 return ret;
2863 }
2864
2865 generate_ppconst(cctx, ppconst);
2866 return generate_COMPARE(cctx, type, ic);
2867 }
2868
2869 return OK;
2870}
2871
2872static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2873
2874/*
2875 * Compile || or &&.
2876 */
2877 static int
2878compile_and_or(
2879 char_u **arg,
2880 cctx_T *cctx,
2881 char *op,
2882 ppconst_T *ppconst,
2883 int ppconst_used UNUSED)
2884{
2885 char_u *next;
2886 char_u *p = may_peek_next_line(cctx, *arg, &next);
2887 int opchar = *op;
2888
2889 if (p[0] == opchar && p[1] == opchar)
2890 {
2891 garray_T *instr = &cctx->ctx_instr;
2892 garray_T end_ga;
2893 int save_skip = cctx->ctx_skip;
2894
2895 /*
2896 * Repeat until there is no following "||" or "&&"
2897 */
2898 ga_init2(&end_ga, sizeof(int), 10);
2899 while (p[0] == opchar && p[1] == opchar)
2900 {
2901 long start_lnum = SOURCING_LNUM;
2902 long save_sourcing_lnum;
2903 int start_ctx_lnum = cctx->ctx_lnum;
2904 int save_lnum;
2905 int const_used;
2906 int status;
2907 jumpwhen_T jump_when = opchar == '|'
2908 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2909
2910 if (next != NULL)
2911 {
2912 *arg = next_line_from_context(cctx, TRUE);
2913 p = skipwhite(*arg);
2914 }
2915
2916 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2917 {
2918 semsg(_(e_white_space_required_before_and_after_str_at_str),
2919 op, p);
2920 ga_clear(&end_ga);
2921 return FAIL;
2922 }
2923
2924 save_sourcing_lnum = SOURCING_LNUM;
2925 SOURCING_LNUM = start_lnum;
2926 save_lnum = cctx->ctx_lnum;
2927 cctx->ctx_lnum = start_ctx_lnum;
2928
2929 status = check_ppconst_bool(ppconst);
2930 if (status != FAIL)
2931 {
2932 // Use the last ppconst if possible.
2933 if (ppconst->pp_used > 0)
2934 {
2935 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2936 int is_true = tv2bool(tv);
2937
2938 if ((is_true && opchar == '|')
2939 || (!is_true && opchar == '&'))
2940 {
2941 // For "false && expr" and "true || expr" the "expr"
2942 // does not need to be evaluated.
2943 cctx->ctx_skip = SKIP_YES;
2944 clear_tv(tv);
2945 tv->v_type = VAR_BOOL;
2946 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2947 }
2948 else
2949 {
2950 // For "true && expr" and "false || expr" only "expr"
2951 // needs to be evaluated.
2952 --ppconst->pp_used;
2953 jump_when = JUMP_NEVER;
2954 }
2955 }
2956 else
2957 {
2958 // Every part must evaluate to a bool.
2959 status = bool_on_stack(cctx);
2960 }
2961 }
2962 if (status != FAIL)
2963 status = ga_grow(&end_ga, 1);
2964 cctx->ctx_lnum = save_lnum;
2965 if (status == FAIL)
2966 {
2967 ga_clear(&end_ga);
2968 return FAIL;
2969 }
2970
2971 if (jump_when != JUMP_NEVER)
2972 {
2973 if (cctx->ctx_skip != SKIP_YES)
2974 {
2975 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2976 ++end_ga.ga_len;
2977 }
2978 generate_JUMP(cctx, jump_when, 0);
2979 }
2980
2981 // eval the next expression
2982 SOURCING_LNUM = save_sourcing_lnum;
2983 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2984 {
2985 ga_clear(&end_ga);
2986 return FAIL;
2987 }
2988
2989 const_used = ppconst->pp_used;
2990 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
2991 : compile_expr4(arg, cctx, ppconst)) == FAIL)
2992 {
2993 ga_clear(&end_ga);
2994 return FAIL;
2995 }
2996
2997 // "0 || 1" results in true, "1 && 0" results in false.
2998 if (ppconst->pp_used == const_used + 1)
2999 {
3000 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3001
3002 if (tv->v_type == VAR_NUMBER
3003 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3004 {
3005 tv->vval.v_number = tv->vval.v_number == 1
3006 ? VVAL_TRUE : VVAL_FALSE;
3007 tv->v_type = VAR_BOOL;
3008 }
3009 }
3010
3011 p = may_peek_next_line(cctx, *arg, &next);
3012 }
3013
3014 if (check_ppconst_bool(ppconst) == FAIL)
3015 {
3016 ga_clear(&end_ga);
3017 return FAIL;
3018 }
3019
3020 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3021 // Every part must evaluate to a bool.
3022 if (bool_on_stack(cctx) == FAIL)
3023 {
3024 ga_clear(&end_ga);
3025 return FAIL;
3026 }
3027
3028 if (end_ga.ga_len > 0)
3029 {
3030 // Fill in the end label in all jumps.
3031 generate_ppconst(cctx, ppconst);
3032 while (end_ga.ga_len > 0)
3033 {
3034 isn_T *isn;
3035
3036 --end_ga.ga_len;
3037 isn = ((isn_T *)instr->ga_data)
3038 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3039 isn->isn_arg.jump.jump_where = instr->ga_len;
3040 }
3041 }
3042 ga_clear(&end_ga);
3043
3044 cctx->ctx_skip = save_skip;
3045 }
3046
3047 return OK;
3048}
3049
3050/*
3051 * expr4a && expr4a && expr4a logical AND
3052 *
3053 * Produces instructions:
3054 * EVAL expr4a Push result of "expr4a"
3055 * COND2BOOL convert to bool if needed
3056 * JUMP_IF_COND_FALSE end
3057 * EVAL expr4b Push result of "expr4b"
3058 * JUMP_IF_COND_FALSE end
3059 * EVAL expr4c Push result of "expr4c"
3060 * end:
3061 */
3062 static int
3063compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3064{
3065 int ppconst_used = ppconst->pp_used;
3066
3067 // get the first variable
3068 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3069 return FAIL;
3070
3071 // || and && work almost the same
3072 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3073}
3074
3075/*
3076 * expr3a || expr3b || expr3c logical OR
3077 *
3078 * Produces instructions:
3079 * EVAL expr3a Push result of "expr3a"
3080 * COND2BOOL convert to bool if needed
3081 * JUMP_IF_COND_TRUE end
3082 * EVAL expr3b Push result of "expr3b"
3083 * JUMP_IF_COND_TRUE end
3084 * EVAL expr3c Push result of "expr3c"
3085 * end:
3086 */
3087 static int
3088compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3089{
3090 int ppconst_used = ppconst->pp_used;
3091
3092 // eval the first expression
3093 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3094 return FAIL;
3095
3096 // || and && work almost the same
3097 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3098}
3099
3100/*
3101 * Toplevel expression: expr2 ? expr1a : expr1b
3102 * Produces instructions:
3103 * EVAL expr2 Push result of "expr2"
3104 * JUMP_IF_FALSE alt jump if false
3105 * EVAL expr1a
3106 * JUMP_ALWAYS end
3107 * alt: EVAL expr1b
3108 * end:
3109 *
3110 * Toplevel expression: expr2 ?? expr1
3111 * Produces instructions:
3112 * EVAL expr2 Push result of "expr2"
3113 * JUMP_AND_KEEP_IF_TRUE end jump if true
3114 * EVAL expr1
3115 * end:
3116 */
3117 int
3118compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3119{
3120 char_u *p;
3121 int ppconst_used = ppconst->pp_used;
3122 char_u *next;
3123
3124 // Ignore all kinds of errors when not producing code.
3125 if (cctx->ctx_skip == SKIP_YES)
3126 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003127 int prev_did_emsg = did_emsg;
3128
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003129 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003130 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003131 }
3132
3133 // Evaluate the first expression.
3134 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3135 return FAIL;
3136
3137 p = may_peek_next_line(cctx, *arg, &next);
3138 if (*p == '?')
3139 {
3140 int op_falsy = p[1] == '?';
3141 garray_T *instr = &cctx->ctx_instr;
3142 garray_T *stack = &cctx->ctx_type_stack;
3143 int alt_idx = instr->ga_len;
3144 int end_idx = 0;
3145 isn_T *isn;
3146 type_T *type1 = NULL;
3147 int has_const_expr = FALSE;
3148 int const_value = FALSE;
3149 int save_skip = cctx->ctx_skip;
3150
3151 if (next != NULL)
3152 {
3153 *arg = next_line_from_context(cctx, TRUE);
3154 p = skipwhite(*arg);
3155 }
3156
3157 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3158 {
3159 semsg(_(e_white_space_required_before_and_after_str_at_str),
3160 op_falsy ? "??" : "?", p);
3161 return FAIL;
3162 }
3163
3164 if (ppconst->pp_used == ppconst_used + 1)
3165 {
3166 // the condition is a constant, we know whether the ? or the :
3167 // expression is to be evaluated.
3168 has_const_expr = TRUE;
3169 if (op_falsy)
3170 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3171 else
3172 {
3173 int error = FALSE;
3174
3175 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3176 &error);
3177 if (error)
3178 return FAIL;
3179 }
3180 cctx->ctx_skip = save_skip == SKIP_YES ||
3181 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3182
3183 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3184 // "left ?? right" and "left" is truthy: produce "left"
3185 generate_ppconst(cctx, ppconst);
3186 else
3187 {
3188 clear_tv(&ppconst->pp_tv[ppconst_used]);
3189 --ppconst->pp_used;
3190 }
3191 }
3192 else
3193 {
3194 generate_ppconst(cctx, ppconst);
3195 if (op_falsy)
3196 end_idx = instr->ga_len;
3197 generate_JUMP(cctx, op_falsy
3198 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3199 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003200 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003201 }
3202
3203 // evaluate the second expression; any type is accepted
3204 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3205 return FAIL;
3206 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3207 return FAIL;
3208
3209 if (!has_const_expr)
3210 {
3211 generate_ppconst(cctx, ppconst);
3212
3213 if (!op_falsy)
3214 {
3215 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003216 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003217 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003218
3219 end_idx = instr->ga_len;
3220 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3221
3222 // jump here from JUMP_IF_FALSE
3223 isn = ((isn_T *)instr->ga_data) + alt_idx;
3224 isn->isn_arg.jump.jump_where = instr->ga_len;
3225 }
3226 }
3227
3228 if (!op_falsy)
3229 {
3230 // Check for the ":".
3231 p = may_peek_next_line(cctx, *arg, &next);
3232 if (*p != ':')
3233 {
3234 emsg(_(e_missing_colon_after_questionmark));
3235 return FAIL;
3236 }
3237 if (next != NULL)
3238 {
3239 *arg = next_line_from_context(cctx, TRUE);
3240 p = skipwhite(*arg);
3241 }
3242
3243 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3244 {
3245 semsg(_(e_white_space_required_before_and_after_str_at_str),
3246 ":", p);
3247 return FAIL;
3248 }
3249
3250 // evaluate the third expression
3251 if (has_const_expr)
3252 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3253 ? SKIP_YES : SKIP_NOT;
3254 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3255 return FAIL;
3256 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3257 return FAIL;
3258 }
3259
3260 if (!has_const_expr)
3261 {
3262 type_T **typep;
3263
3264 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003265 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003266
3267 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003268 typep = &((((type2_T *)stack->ga_data)
3269 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003270 common_type(type1, *typep, typep, cctx->ctx_type_list);
3271
3272 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3273 isn = ((isn_T *)instr->ga_data) + end_idx;
3274 isn->isn_arg.jump.jump_where = instr->ga_len;
3275 }
3276
3277 cctx->ctx_skip = save_skip;
3278 }
3279 return OK;
3280}
3281
3282/*
3283 * Toplevel expression.
3284 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3285 * Returns OK or FAIL.
3286 */
3287 int
3288compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3289{
3290 ppconst_T ppconst;
3291
3292 CLEAR_FIELD(ppconst);
3293 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3294 {
3295 clear_ppconst(&ppconst);
3296 return FAIL;
3297 }
3298 if (is_const != NULL)
3299 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3300 if (generate_ppconst(cctx, &ppconst) == FAIL)
3301 return FAIL;
3302 return OK;
3303}
3304
3305/*
3306 * Toplevel expression.
3307 */
3308 int
3309compile_expr0(char_u **arg, cctx_T *cctx)
3310{
3311 return compile_expr0_ext(arg, cctx, NULL);
3312}
3313
3314
3315#endif // defined(FEAT_EVAL)