blob: 7a089e973347e60d6efead9b23928aa76464a898 [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 Moolenaaradbc08f2022-11-06 18:27:17 +0000188
189 // a copy is made, the composite is no longer "const"
190 if (typep->type_curr->tt_flags & TTFLAG_CONST)
191 {
192 type_T *type = copy_type(typep->type_curr, cctx->ctx_type_list);
193
194 if (type != typep->type_curr) // did get a copy
195 {
196 type->tt_flags &= ~(TTFLAG_CONST | TTFLAG_STATIC);
197 typep->type_curr = type;
198 }
199 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000200 }
201 else
202 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000203 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000204 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000205 typep->type_curr = typep->type_curr->tt_member;
206 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000207 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000208 typep->type_curr = &t_any;
209 if (typep->type_decl->tt_type == VAR_LIST)
210 {
211 typep->type_decl = typep->type_decl->tt_member;
212 if (typep->type_decl == &t_unknown)
213 // empty list was used
214 typep->type_decl = &t_any;
215 }
216 else
217 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000218 }
219 if (generate_instr_drop(cctx,
220 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
221 == FAIL)
222 return FAIL;
223 }
224 }
225 else
226 {
227 switch (vartype)
228 {
229 case VAR_FUNC:
230 case VAR_PARTIAL:
231 emsg(_(e_cannot_index_a_funcref));
232 break;
233 case VAR_BOOL:
234 case VAR_SPECIAL:
235 case VAR_JOB:
236 case VAR_CHANNEL:
237 case VAR_INSTR:
238 case VAR_UNKNOWN:
239 case VAR_ANY:
240 case VAR_VOID:
241 emsg(_(e_cannot_index_special_variable));
242 break;
243 default:
244 emsg(_(e_string_list_dict_or_blob_required));
245 }
246 return FAIL;
247 }
248 return OK;
249}
250
251/*
252 * Generate an instruction to load script-local variable "name", without the
253 * leading "s:".
254 * Also finds imported variables.
255 */
256 int
257compile_load_scriptvar(
258 cctx_T *cctx,
259 char_u *name, // variable NUL terminated
260 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100261 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000262{
263 scriptitem_T *si;
264 int idx;
265 imported_T *import;
266
267 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
268 return FAIL;
269 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000270 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000271 if (idx >= 0)
272 {
273 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
274
275 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
276 current_sctx.sc_sid, idx, sv->sv_type);
277 return OK;
278 }
279
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000280 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000281 if (import != NULL)
282 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000283 char_u *p = skipwhite(*end);
284 char_u *exp_name;
285 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100286 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000287 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000288 int done = FALSE;
289 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000290
291 // Need to lookup the member.
292 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000293 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000294 semsg(_(e_expected_dot_after_name_str), start);
295 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000296 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000297 ++p;
298 if (VIM_ISWHITE(*p))
299 {
300 emsg(_(e_no_white_space_allowed_after_dot));
301 return FAIL;
302 }
303
304 // isolate one name
305 exp_name = p;
306 while (eval_isnamec(*p))
307 ++p;
308 cc = *p;
309 *p = NUL;
310
Bram Moolenaard041f422022-01-12 19:54:00 +0000311 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100312 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
313 // "import autoload './dir/script.vim'" or
314 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100315 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100316
317 if (res == OK)
318 {
319 if (si->sn_autoload_prefix != NULL
320 && si->sn_state == SN_STATE_NOT_LOADED)
321 {
322 char_u *auto_name =
323 concat_str(si->sn_autoload_prefix, exp_name);
324
325 // autoload script must be loaded later, access by the autoload
326 // name. If a '(' follows it must be a function. Otherwise we
327 // don't know, it can be "script.Func".
328 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100329 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100330 else
331 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
332 vim_free(auto_name);
333 done = TRUE;
334 }
335 else if (si->sn_import_autoload
336 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100337 {
338 // If a '(' follows it must be a function. Otherwise we don't
339 // know, it can be "script.Func".
340 if (cc == '(' || paren_follows_after_expr)
341 {
342 char_u sid_name[MAX_FUNC_NAME_LEN];
343
344 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100345 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100346 }
347 else
348 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
349 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100350 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100351 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100352 else
353 {
354 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
355 cctx, NULL, TRUE);
356 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100357 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100358
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000359 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000360 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000361 if (done)
362 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000363
364 if (idx < 0)
365 {
366 if (ufunc != NULL)
367 {
368 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100369 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000370 return OK;
371 }
372 return FAIL;
373 }
374
375 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
376 import->imp_sid,
377 idx,
378 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000379 return OK;
380 }
381
Bram Moolenaard0132f42022-05-12 11:05:40 +0100382 // Can only get here if we know "name" is a script variable and not in a
383 // Vim9 script (variable is not in sn_var_vals): old style script.
384 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000385 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000386}
387
388 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000389generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000390{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000391 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000392 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000393
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000394 // Reject a global non-autoload function found without the "g:" prefix.
395 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000396 return FAIL;
397
398 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000399 compile_type = get_compile_type(ufunc);
400 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000401 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000402 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100403 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000404}
405
406/*
407 * Compile a variable name into a load instruction.
408 * "end" points to just after the name.
409 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
410 * When "error" is FALSE do not give an error when not found.
411 */
412 int
413compile_load(
414 char_u **arg,
415 char_u *end_arg,
416 cctx_T *cctx,
417 int is_expr,
418 int error)
419{
420 type_T *type;
421 char_u *name = NULL;
422 char_u *end = end_arg;
423 int res = FAIL;
424 int prev_called_emsg = called_emsg;
425
426 if (*(*arg + 1) == ':')
427 {
428 if (end <= *arg + 2)
429 {
430 isntype_T isn_type;
431
432 // load dictionary of namespace
433 switch (**arg)
434 {
435 case 'g': isn_type = ISN_LOADGDICT; break;
436 case 'w': isn_type = ISN_LOADWDICT; break;
437 case 't': isn_type = ISN_LOADTDICT; break;
438 case 'b': isn_type = ISN_LOADBDICT; break;
439 default:
440 semsg(_(e_namespace_not_supported_str), *arg);
441 goto theend;
442 }
443 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
444 goto theend;
445 res = OK;
446 }
447 else
448 {
449 isntype_T isn_type = ISN_DROP;
450
451 // load namespaced variable
452 name = vim_strnsave(*arg + 2, end - (*arg + 2));
453 if (name == NULL)
454 return FAIL;
455
456 switch (**arg)
457 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100458 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000459 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000460 case 's': if (current_script_is_vim9())
461 {
462 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
463 *arg);
464 vim_free(name);
465 return FAIL;
466 }
Kota Kato948a3892022-08-16 16:09:59 +0100467 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000468 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000469 else
470 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100471 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000472 break;
473 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
474 {
475 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000476 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000477 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000478 else
479 isn_type = ISN_LOADG;
480 }
481 else
482 {
483 isn_type = ISN_LOADAUTO;
484 vim_free(name);
485 name = vim_strnsave(*arg, end - *arg);
486 if (name == NULL)
487 return FAIL;
488 }
489 break;
490 case 'w': isn_type = ISN_LOADW; break;
491 case 't': isn_type = ISN_LOADT; break;
492 case 'b': isn_type = ISN_LOADB; break;
493 default: // cannot happen, just in case
494 semsg(_(e_namespace_not_supported_str), *arg);
495 goto theend;
496 }
497 if (isn_type != ISN_DROP)
498 {
499 // Global, Buffer-local, Window-local and Tabpage-local
500 // variables can be defined later, thus we don't check if it
501 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000502 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000503 }
504 }
505 }
506 else
507 {
508 size_t len = end - *arg;
509 int idx;
510 int gen_load = FALSE;
511 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100512 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100513 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000514
515 name = vim_strnsave(*arg, end - *arg);
516 if (name == NULL)
517 return FAIL;
518
519 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
520 {
521 script_autoload(name, FALSE);
522 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
523 }
524 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
525 == OK)
526 {
527 if (gen_load_outer == 0)
528 gen_load = TRUE;
529 }
530 else
531 {
532 lvar_T lvar;
533
534 if (lookup_local(*arg, len, &lvar, cctx) == OK)
535 {
536 type = lvar.lv_type;
537 idx = lvar.lv_idx;
538 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100539 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000540 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100541 outer_loop_depth = lvar.lv_loop_depth;
542 outer_loop_idx = lvar.lv_loop_idx;
543 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000544 else
545 gen_load = TRUE;
546 }
547 else
548 {
549 // "var" can be script-local even without using "s:" if it
550 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000551 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000552 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100553 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000554
555 // When evaluating an expression and the name starts with an
556 // uppercase letter it can be a user defined function.
557 // generate_funcref() will fail if the function can't be found.
558 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000559 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000560 }
561 }
562 if (gen_load)
563 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
564 if (gen_load_outer > 0)
565 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100566 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
567 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000568 cctx->ctx_outer_used = TRUE;
569 }
570 }
571
572 *arg = end;
573
574theend:
575 if (res == FAIL && error && called_emsg == prev_called_emsg)
576 semsg(_(e_variable_not_found_str), name);
577 vim_free(name);
578 return res;
579}
580
581/*
582 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100583 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000584 * Returns FAIL if compilation fails.
585 */
586 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100587compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000588{
LemonBoyf3b48952022-05-05 13:53:03 +0100589 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000590 garray_T save_ga = cctx->ctx_instr;
591 int expr_res;
592 int trailing_error;
593 int instr_count;
594 isn_T *instr = NULL;
595
596 // Remove the string type from the stack.
597 --cctx->ctx_type_stack.ga_len;
598
599 // Temporarily reset the list of instructions so that the jump labels are
600 // correct.
601 cctx->ctx_instr.ga_len = 0;
602 cctx->ctx_instr.ga_maxlen = 0;
603 cctx->ctx_instr.ga_data = NULL;
604 expr_res = compile_expr0(&s, cctx);
605 s = skipwhite(s);
606 trailing_error = *s != NUL;
607
608 if (expr_res == FAIL || trailing_error
609 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
610 {
611 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000612 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000613 clear_instr_ga(&cctx->ctx_instr);
614 cctx->ctx_instr = save_ga;
615 ++cctx->ctx_type_stack.ga_len;
616 return FAIL;
617 }
618
619 // Move the generated instructions into the ISN_INSTR instruction, then
620 // restore the list of instructions.
621 instr_count = cctx->ctx_instr.ga_len;
622 instr = cctx->ctx_instr.ga_data;
623 instr[instr_count].isn_type = ISN_FINISH;
624
625 cctx->ctx_instr = save_ga;
626 vim_free(isn->isn_arg.string);
627 isn->isn_type = ISN_INSTR;
628 isn->isn_arg.instr = instr;
629 return OK;
630}
631
632/*
633 * Compile the argument expressions.
634 * "arg" points to just after the "(" and is advanced to after the ")"
635 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100636 int
LemonBoyf3b48952022-05-05 13:53:03 +0100637compile_arguments(
638 char_u **arg,
639 cctx_T *cctx,
640 int *argcount,
641 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000642{
643 char_u *p = *arg;
644 char_u *whitep = *arg;
645 int must_end = FALSE;
646 int instr_count;
647
648 for (;;)
649 {
650 if (may_get_next_line(whitep, &p, cctx) == FAIL)
651 goto failret;
652 if (*p == ')')
653 {
654 *arg = p + 1;
655 return OK;
656 }
657 if (must_end)
658 {
659 semsg(_(e_missing_comma_before_argument_str), p);
660 return FAIL;
661 }
662
663 instr_count = cctx->ctx_instr.ga_len;
664 if (compile_expr0(&p, cctx) == FAIL)
665 return FAIL;
666 ++*argcount;
667
LemonBoyf3b48952022-05-05 13:53:03 +0100668 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000669 && cctx->ctx_instr.ga_len == instr_count + 1)
670 {
671 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
672
673 // {skip} argument of searchpair() can be compiled if not empty
674 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100675 compile_string(isn, cctx, 0);
676 }
677 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
678 && cctx->ctx_instr.ga_len == instr_count + 1)
679 {
680 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
681
682 // {sub} argument of substitute() can be compiled if it starts
683 // with \=
684 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100685 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100686 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000687 }
688
689 if (*p != ',' && *skipwhite(p) == ',')
690 {
691 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
692 p = skipwhite(p);
693 }
694 if (*p == ',')
695 {
696 ++p;
697 if (*p != NUL && !VIM_ISWHITE(*p))
698 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
699 }
700 else
701 must_end = TRUE;
702 whitep = p;
703 p = skipwhite(p);
704 }
705failret:
706 emsg(_(e_missing_closing_paren));
707 return FAIL;
708}
709
710/*
711 * Compile a function call: name(arg1, arg2)
712 * "arg" points to "name", "arg + varlen" to the "(".
713 * "argcount_init" is 1 for "value->method()"
714 * Instructions:
715 * EVAL arg1
716 * EVAL arg2
717 * BCALL / DCALL / UCALL
718 */
719 static int
720compile_call(
721 char_u **arg,
722 size_t varlen,
723 cctx_T *cctx,
724 ppconst_T *ppconst,
725 int argcount_init)
726{
727 char_u *name = *arg;
728 char_u *p;
729 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100730 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000731 char_u fname_buf[FLEN_FIXED + 1];
732 char_u *tofree = NULL;
733 int error = FCERR_NONE;
734 ufunc_T *ufunc = NULL;
735 int res = FAIL;
736 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000737 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100738 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000739 imported_T *import;
740
741 if (varlen >= sizeof(namebuf))
742 {
743 semsg(_(e_name_too_long_str), name);
744 return FAIL;
745 }
746 vim_strncpy(namebuf, *arg, varlen);
747
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000748 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000749 if (import != NULL)
750 {
751 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
752 return FAIL;
753 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000754
755 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100756 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000757 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100758 if ((varlen == 3
759 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000760 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
761 {
762 char_u *s = skipwhite(*arg + varlen + 1);
763 typval_T argvars[2];
764 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100765 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000766
767 argvars[0].v_type = VAR_UNKNOWN;
768 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100769 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000770 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100771 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 s = skipwhite(s);
773 if (*s == ')' && argvars[0].v_type == VAR_STRING
774 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
775 || !is_has))
776 {
777 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
778
779 *arg = s + 1;
780 argvars[1].v_type = VAR_UNKNOWN;
781 tv->v_type = VAR_NUMBER;
782 tv->vval.v_number = 0;
783 if (is_has)
784 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100785 else if (is_len)
786 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000787 else
788 f_exists(argvars, tv);
789 clear_tv(&argvars[0]);
790 ++ppconst->pp_used;
791 return OK;
792 }
793 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100794 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000795 {
796 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
797 return FAIL;
798 }
799 }
800
801 if (generate_ppconst(cctx, ppconst) == FAIL)
802 return FAIL;
803
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000804 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
805
806 // We handle the "skip" argument of searchpair() and searchpairpos()
807 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100808 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
809 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
810 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
811 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
812 special_fn = CA_SEARCHPAIR;
813 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
814 special_fn = CA_SUBSTITUTE;
815 else
816 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000817
818 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100819 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000820 goto theend;
821
822 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
823 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
824 {
825 int idx;
826
827 // builtin function
828 idx = find_internal_func(name);
829 if (idx >= 0)
830 {
831 if (STRCMP(name, "flatten") == 0)
832 {
833 emsg(_(e_cannot_use_flatten_in_vim9_script));
834 goto theend;
835 }
836
837 if (STRCMP(name, "add") == 0 && argcount == 2)
838 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000839 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000840
841 // add() can be compiled to instructions if we know the type
842 if (type->tt_type == VAR_LIST)
843 {
844 // inline "add(list, item)" so that the type can be checked
845 res = generate_LISTAPPEND(cctx);
846 idx = -1;
847 }
848 else if (type->tt_type == VAR_BLOB)
849 {
850 // inline "add(blob, nr)" so that the type can be checked
851 res = generate_BLOBAPPEND(cctx);
852 idx = -1;
853 }
854 }
855
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100856 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
857 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100858 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100859 // May have the "D" or "R" flag, reserve a variable for a
860 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100861 if (get_defer_var_idx(cctx) == 0)
862 idx = -1;
863 }
864
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000865 if (idx >= 0)
866 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
867 }
868 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100869 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000870 goto theend;
871 }
872
Bram Moolenaar62aec932022-01-29 21:45:34 +0000873 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
874
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000875 // An argument or local variable can be a function reference, this
876 // overrules a function name.
877 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
878 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
879 {
880 // If we can find the function by name generate the right call.
881 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000882 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000883 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000884 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000885 if (!func_is_global(ufunc))
886 {
887 res = generate_CALL(cctx, ufunc, argcount);
888 goto theend;
889 }
890 if (!has_g_namespace
891 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
892 {
893 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100894 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000895 goto theend;
896 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 }
898 }
899
900 // If the name is a variable, load it and use PCALL.
901 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000902 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000903 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000904 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000905 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
906 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000907 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000908
909 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
910 goto theend;
911 }
912
913 // If we can find a global function by name generate the right call.
914 if (ufunc != NULL)
915 {
916 res = generate_CALL(cctx, ufunc, argcount);
917 goto theend;
918 }
919
920 // A global function may be defined only later. Need to figure out at
921 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000922 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000923 res = generate_UCALL(cctx, name, argcount);
924 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100925 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000926
927theend:
928 vim_free(tofree);
929 return res;
930}
931
932// like NAMESPACE_CHAR but with 'a' and 'l'.
933#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
934
935/*
936 * Find the end of a variable or function name. Unlike find_name_end() this
937 * does not recognize magic braces.
938 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
939 * Return a pointer to just after the name. Equal to "arg" if there is no
940 * valid name.
941 */
942 char_u *
943to_name_end(char_u *arg, int use_namespace)
944{
945 char_u *p;
946
947 // Quick check for valid starting character.
948 if (!eval_isnamec1(*arg))
949 return arg;
950
951 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
952 // Include a namespace such as "s:var" and "v:var". But "n:" is not
953 // and can be used in slice "[n:]".
954 if (*p == ':' && (p != arg + 1
955 || !use_namespace
956 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
957 break;
958 return p;
959}
960
961/*
962 * Like to_name_end() but also skip over a list or dict constant.
963 * Also accept "<SNR>123_Func".
964 * This intentionally does not handle line continuation.
965 */
966 char_u *
967to_name_const_end(char_u *arg)
968{
969 char_u *p = arg;
970 typval_T rettv;
971
972 if (STRNCMP(p, "<SNR>", 5) == 0)
973 p = skipdigits(p + 5);
974 p = to_name_end(p, TRUE);
975 if (p == arg && *arg == '[')
976 {
977
978 // Can be "[1, 2, 3]->Func()".
979 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
980 p = arg;
981 }
982 return p;
983}
984
985/*
986 * parse a list: [expr, expr]
987 * "*arg" points to the '['.
988 * ppconst->pp_is_const is set if all items are a constant.
989 */
990 static int
991compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
992{
993 char_u *p = skipwhite(*arg + 1);
994 char_u *whitep = *arg + 1;
995 int count = 0;
996 int is_const;
997 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +0100998 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000999
1000 for (;;)
1001 {
1002 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1003 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001004 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001005 return FAIL;
1006 }
1007 if (*p == ',')
1008 {
1009 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1010 return FAIL;
1011 }
1012 if (*p == ']')
1013 {
1014 ++p;
1015 break;
1016 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001017 if (must_end)
1018 {
1019 semsg(_(e_missing_comma_in_list_str), p);
1020 return FAIL;
1021 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001022 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1023 return FAIL;
1024 if (!is_const)
1025 is_all_const = FALSE;
1026 ++count;
1027 if (*p == ',')
1028 {
1029 ++p;
1030 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1031 {
1032 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1033 return FAIL;
1034 }
1035 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001036 else
1037 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001038 whitep = p;
1039 p = skipwhite(p);
1040 }
1041 *arg = p;
1042
1043 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001044 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001045}
1046
1047/*
1048 * Parse a lambda: "(arg, arg) => expr"
1049 * "*arg" points to the '('.
1050 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1051 */
1052 static int
1053compile_lambda(char_u **arg, cctx_T *cctx)
1054{
1055 int r;
1056 typval_T rettv;
1057 ufunc_T *ufunc;
1058 evalarg_T evalarg;
1059
1060 init_evalarg(&evalarg);
1061 evalarg.eval_flags = EVAL_EVALUATE;
1062 evalarg.eval_cctx = cctx;
1063
1064 // Get the funcref in "rettv".
1065 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1066 if (r != OK)
1067 {
1068 clear_evalarg(&evalarg, NULL);
1069 return r;
1070 }
1071
1072 // "rettv" will now be a partial referencing the function.
1073 ufunc = rettv.vval.v_partial->pt_func;
1074 ++ufunc->uf_refcount;
1075 clear_tv(&rettv);
1076
1077 // Compile it here to get the return type. The return type is optional,
1078 // when it's missing use t_unknown. This is recognized in
1079 // compile_return().
1080 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1081 ufunc->uf_ret_type = &t_unknown;
1082 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1083
1084 // When the outer function is compiled for profiling or debugging, the
1085 // lambda may be called without profiling or debugging. Compile it here in
1086 // the right context.
1087 if (cctx->ctx_compile_type == CT_DEBUG
1088#ifdef FEAT_PROFILE
1089 || cctx->ctx_compile_type == CT_PROFILE
1090#endif
1091 )
1092 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1093
Bram Moolenaar139575d2022-03-15 19:29:30 +00001094 // if the outer function is not compiled for debugging or profiling, this
1095 // one might be
1096 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001097 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001098 compiletype_T compile_type = get_compile_type(ufunc);
1099
1100 if (compile_type != CT_NONE)
1101 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001102 }
1103
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001104 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1105 // "*arg" may point into it. Point into the original line to avoid a
1106 // dangling pointer.
1107 if (evalarg.eval_using_cmdline)
1108 {
1109 garray_T *gap = &evalarg.eval_tofree_ga;
1110 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1111
1112 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1113 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001114 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001115 }
1116
1117 clear_evalarg(&evalarg, NULL);
1118
1119 if (ufunc->uf_def_status == UF_COMPILED)
1120 {
1121 // The return type will now be known.
1122 set_function_type(ufunc);
1123
1124 // The function reference count will be 1. When the ISN_FUNCREF
1125 // instruction is deleted the reference count is decremented and the
1126 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001127 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001128 }
1129
1130 func_ptr_unref(ufunc);
1131 return FAIL;
1132}
1133
1134/*
1135 * Get a lambda and compile it. Uses Vim9 syntax.
1136 */
1137 int
1138get_lambda_tv_and_compile(
1139 char_u **arg,
1140 typval_T *rettv,
1141 int types_optional,
1142 evalarg_T *evalarg)
1143{
1144 int r;
1145 ufunc_T *ufunc;
1146 int save_sc_version = current_sctx.sc_version;
1147
1148 // Get the funcref in "rettv".
1149 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1150 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1151 current_sctx.sc_version = save_sc_version;
1152 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001153 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001154
1155 // "rettv" will now be a partial referencing the function.
1156 ufunc = rettv->vval.v_partial->pt_func;
1157
1158 // Compile it here to get the return type. The return type is optional,
1159 // when it's missing use t_unknown. This is recognized in
1160 // compile_return().
1161 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1162 ufunc->uf_ret_type = &t_unknown;
1163 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1164
1165 if (ufunc->uf_def_status == UF_COMPILED)
1166 {
1167 // The return type will now be known.
1168 set_function_type(ufunc);
1169 return OK;
1170 }
1171 clear_tv(rettv);
1172 return FAIL;
1173}
1174
1175/*
1176 * parse a dict: {key: val, [key]: val}
1177 * "*arg" points to the '{'.
1178 * ppconst->pp_is_const is set if all item values are a constant.
1179 */
1180 static int
1181compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1182{
1183 garray_T *instr = &cctx->ctx_instr;
1184 int count = 0;
1185 dict_T *d = dict_alloc();
1186 dictitem_T *item;
1187 char_u *whitep = *arg + 1;
1188 char_u *p;
1189 int is_const;
1190 int is_all_const = TRUE; // reset when non-const encountered
1191
1192 if (d == NULL)
1193 return FAIL;
1194 if (generate_ppconst(cctx, ppconst) == FAIL)
1195 return FAIL;
1196 for (;;)
1197 {
1198 char_u *key = NULL;
1199
1200 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1201 {
1202 *arg = NULL;
1203 goto failret;
1204 }
1205
1206 if (**arg == '}')
1207 break;
1208
1209 if (**arg == '[')
1210 {
1211 isn_T *isn;
1212
1213 // {[expr]: value} uses an evaluated key.
1214 *arg = skipwhite(*arg + 1);
1215 if (compile_expr0(arg, cctx) == FAIL)
1216 return FAIL;
1217 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1218 if (isn->isn_type == ISN_PUSHNR)
1219 {
1220 char buf[NUMBUFLEN];
1221
1222 // Convert to string at compile time.
1223 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1224 isn->isn_type = ISN_PUSHS;
1225 isn->isn_arg.string = vim_strsave((char_u *)buf);
1226 }
1227 if (isn->isn_type == ISN_PUSHS)
1228 key = isn->isn_arg.string;
1229 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1230 return FAIL;
1231 *arg = skipwhite(*arg);
1232 if (**arg != ']')
1233 {
1234 emsg(_(e_missing_matching_bracket_after_dict_key));
1235 return FAIL;
1236 }
1237 ++*arg;
1238 }
1239 else
1240 {
1241 // {"name": value},
1242 // {'name': value},
1243 // {name: value} use "name" as a literal key
1244 key = get_literal_key(arg);
1245 if (key == NULL)
1246 return FAIL;
1247 if (generate_PUSHS(cctx, &key) == FAIL)
1248 return FAIL;
1249 }
1250
1251 // Check for duplicate keys, if using string keys.
1252 if (key != NULL)
1253 {
1254 item = dict_find(d, key, -1);
1255 if (item != NULL)
1256 {
dundargocc57b5bc2022-11-02 13:30:51 +00001257 semsg(_(e_duplicate_key_in_dictionary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001258 goto failret;
1259 }
1260 item = dictitem_alloc(key);
1261 if (item != NULL)
1262 {
1263 item->di_tv.v_type = VAR_UNKNOWN;
1264 item->di_tv.v_lock = 0;
1265 if (dict_add(d, item) == FAIL)
1266 dictitem_free(item);
1267 }
1268 }
1269
1270 if (**arg != ':')
1271 {
1272 if (*skipwhite(*arg) == ':')
1273 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1274 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001275 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001276 return FAIL;
1277 }
1278 whitep = *arg + 1;
1279 if (!IS_WHITE_OR_NUL(*whitep))
1280 {
1281 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1282 return FAIL;
1283 }
1284
1285 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1286 {
1287 *arg = NULL;
1288 goto failret;
1289 }
1290
1291 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1292 return FAIL;
1293 if (!is_const)
1294 is_all_const = FALSE;
1295 ++count;
1296
1297 whitep = *arg;
1298 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1299 {
1300 *arg = NULL;
1301 goto failret;
1302 }
1303 if (**arg == '}')
1304 break;
1305 if (**arg != ',')
1306 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001307 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001308 goto failret;
1309 }
1310 if (IS_WHITE_OR_NUL(*whitep))
1311 {
1312 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1313 return FAIL;
1314 }
1315 whitep = *arg + 1;
1316 if (!IS_WHITE_OR_NUL(*whitep))
1317 {
1318 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1319 return FAIL;
1320 }
1321 *arg = skipwhite(whitep);
1322 }
1323
1324 *arg = *arg + 1;
1325
1326 // Allow for following comment, after at least one space.
1327 p = skipwhite(*arg);
1328 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1329 *arg += STRLEN(*arg);
1330
1331 dict_unref(d);
1332 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001333 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001334
1335failret:
1336 if (*arg == NULL)
1337 {
1338 semsg(_(e_missing_dict_end), _("[end of lines]"));
1339 *arg = (char_u *)"";
1340 }
1341 dict_unref(d);
1342 return FAIL;
1343}
1344
1345/*
1346 * Compile "&option".
1347 */
1348 static int
1349compile_get_option(char_u **arg, cctx_T *cctx)
1350{
1351 typval_T rettv;
1352 char_u *start = *arg;
1353 int ret;
1354
1355 // parse the option and get the current value to get the type.
1356 rettv.v_type = VAR_UNKNOWN;
1357 ret = eval_option(arg, &rettv, TRUE);
1358 if (ret == OK)
1359 {
1360 // include the '&' in the name, eval_option() expects it.
1361 char_u *name = vim_strnsave(start, *arg - start);
1362 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1363 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1364
1365 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1366 vim_free(name);
1367 }
1368 clear_tv(&rettv);
1369
1370 return ret;
1371}
1372
1373/*
1374 * Compile "$VAR".
1375 */
1376 static int
1377compile_get_env(char_u **arg, cctx_T *cctx)
1378{
1379 char_u *start = *arg;
1380 int len;
1381 int ret;
1382 char_u *name;
1383
1384 ++*arg;
1385 len = get_env_len(arg);
1386 if (len == 0)
1387 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001388 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001389 return FAIL;
1390 }
1391
1392 // include the '$' in the name, eval_env_var() expects it.
1393 name = vim_strnsave(start, len + 1);
1394 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1395 vim_free(name);
1396 return ret;
1397}
1398
1399/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001400 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001401 */
1402 static int
1403compile_interp_string(char_u **arg, cctx_T *cctx)
1404{
1405 typval_T tv;
1406 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001407 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001408 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001409 int count = 0;
1410 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001411
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001412 // *arg is on the '$' character, move it to the first string character.
1413 ++*arg;
1414 quote = **arg;
1415 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001416
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001417 for (;;)
1418 {
1419 // Get the string up to the matching quote or to a single '{'.
1420 // "arg" is advanced to either the quote or the '{'.
1421 if (quote == '"')
1422 ret = eval_string(arg, &tv, evaluate, TRUE);
1423 else
1424 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1425 if (ret == FAIL)
1426 break;
1427 if (evaluate)
1428 {
1429 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1430 || (**arg != '{' && count == 0))
1431 {
1432 // generate non-empty string or empty string if it's the only
1433 // one
1434 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1435 return FAIL;
1436 tv.vval.v_string = NULL; // don't free it now
1437 ++count;
1438 }
1439 clear_tv(&tv);
1440 }
1441
1442 if (**arg != '{')
1443 {
1444 // found terminating quote
1445 ++*arg;
1446 break;
1447 }
1448
1449 p = compile_one_expr_in_str(*arg, cctx);
1450 if (p == NULL)
1451 {
1452 ret = FAIL;
1453 break;
1454 }
1455 ++count;
1456 *arg = p;
1457 }
LemonBoy2eaef102022-05-06 13:14:50 +01001458
1459 if (ret == FAIL || !evaluate)
1460 return ret;
1461
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001462 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1463 if (count > 1)
1464 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001465
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001466 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001467}
1468
1469/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001470 * Compile "@r".
1471 */
1472 static int
1473compile_get_register(char_u **arg, cctx_T *cctx)
1474{
1475 int ret;
1476
1477 ++*arg;
1478 if (**arg == NUL)
1479 {
1480 semsg(_(e_syntax_error_at_str), *arg - 1);
1481 return FAIL;
1482 }
1483 if (!valid_yank_reg(**arg, FALSE))
1484 {
1485 emsg_invreg(**arg);
1486 return FAIL;
1487 }
1488 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1489 ++*arg;
1490 return ret;
1491}
1492
1493/*
1494 * Apply leading '!', '-' and '+' to constant "rettv".
1495 * When "numeric_only" is TRUE do not apply '!'.
1496 */
1497 static int
1498apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1499{
1500 char_u *p = *end;
1501
1502 // this works from end to start
1503 while (p > start)
1504 {
1505 --p;
1506 if (*p == '-' || *p == '+')
1507 {
1508 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001509 if (rettv->v_type == VAR_FLOAT)
1510 {
1511 if (*p == '-')
1512 rettv->vval.v_float = -rettv->vval.v_float;
1513 }
1514 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001515 {
1516 varnumber_T val;
1517 int error = FALSE;
1518
1519 // tv_get_number_chk() accepts a string, but we don't want that
1520 // here
1521 if (check_not_string(rettv) == FAIL)
1522 return FAIL;
1523 val = tv_get_number_chk(rettv, &error);
1524 clear_tv(rettv);
1525 if (error)
1526 return FAIL;
1527 if (*p == '-')
1528 val = -val;
1529 rettv->v_type = VAR_NUMBER;
1530 rettv->vval.v_number = val;
1531 }
1532 }
1533 else if (numeric_only)
1534 {
1535 ++p;
1536 break;
1537 }
1538 else if (*p == '!')
1539 {
1540 int v = tv2bool(rettv);
1541
1542 // '!' is permissive in the type.
1543 clear_tv(rettv);
1544 rettv->v_type = VAR_BOOL;
1545 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1546 }
1547 }
1548 *end = p;
1549 return OK;
1550}
1551
1552/*
1553 * Recognize v: variables that are constants and set "rettv".
1554 */
1555 static void
1556get_vim_constant(char_u **arg, typval_T *rettv)
1557{
1558 if (STRNCMP(*arg, "v:true", 6) == 0)
1559 {
1560 rettv->v_type = VAR_BOOL;
1561 rettv->vval.v_number = VVAL_TRUE;
1562 *arg += 6;
1563 }
1564 else if (STRNCMP(*arg, "v:false", 7) == 0)
1565 {
1566 rettv->v_type = VAR_BOOL;
1567 rettv->vval.v_number = VVAL_FALSE;
1568 *arg += 7;
1569 }
1570 else if (STRNCMP(*arg, "v:null", 6) == 0)
1571 {
1572 rettv->v_type = VAR_SPECIAL;
1573 rettv->vval.v_number = VVAL_NULL;
1574 *arg += 6;
1575 }
1576 else if (STRNCMP(*arg, "v:none", 6) == 0)
1577 {
1578 rettv->v_type = VAR_SPECIAL;
1579 rettv->vval.v_number = VVAL_NONE;
1580 *arg += 6;
1581 }
1582}
1583
1584 exprtype_T
1585get_compare_type(char_u *p, int *len, int *type_is)
1586{
1587 exprtype_T type = EXPR_UNKNOWN;
1588 int i;
1589
1590 switch (p[0])
1591 {
1592 case '=': if (p[1] == '=')
1593 type = EXPR_EQUAL;
1594 else if (p[1] == '~')
1595 type = EXPR_MATCH;
1596 break;
1597 case '!': if (p[1] == '=')
1598 type = EXPR_NEQUAL;
1599 else if (p[1] == '~')
1600 type = EXPR_NOMATCH;
1601 break;
1602 case '>': if (p[1] != '=')
1603 {
1604 type = EXPR_GREATER;
1605 *len = 1;
1606 }
1607 else
1608 type = EXPR_GEQUAL;
1609 break;
1610 case '<': if (p[1] != '=')
1611 {
1612 type = EXPR_SMALLER;
1613 *len = 1;
1614 }
1615 else
1616 type = EXPR_SEQUAL;
1617 break;
1618 case 'i': if (p[1] == 's')
1619 {
1620 // "is" and "isnot"; but not a prefix of a name
1621 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1622 *len = 5;
1623 i = p[*len];
1624 if (!isalnum(i) && i != '_')
1625 {
1626 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1627 *type_is = TRUE;
1628 }
1629 }
1630 break;
1631 }
1632 return type;
1633}
1634
1635/*
1636 * Skip over an expression, ignoring most errors.
1637 */
1638 void
1639skip_expr_cctx(char_u **arg, cctx_T *cctx)
1640{
1641 evalarg_T evalarg;
1642
1643 init_evalarg(&evalarg);
1644 evalarg.eval_cctx = cctx;
1645 skip_expr(arg, &evalarg);
1646 clear_evalarg(&evalarg, NULL);
1647}
1648
1649/*
1650 * Check that the top of the type stack has a type that can be used as a
1651 * condition. Give an error and return FAIL if not.
1652 */
1653 int
1654bool_on_stack(cctx_T *cctx)
1655{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001656 type_T *type;
1657
Bram Moolenaar078a4612022-01-04 15:17:03 +00001658 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001659 if (type == &t_bool)
1660 return OK;
1661
Bram Moolenaar4913d422022-10-17 13:13:32 +01001662 if (type->tt_type == VAR_ANY
1663 || type->tt_type == VAR_UNKNOWN
1664 || type->tt_type == VAR_NUMBER
1665 || type == &t_number_bool
1666 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001667 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1668 // This requires a runtime type check.
1669 return generate_COND2BOOL(cctx);
1670
1671 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1672}
1673
1674/*
1675 * Give the "white on both sides" error, taking the operator from "p[len]".
1676 */
1677 void
1678error_white_both(char_u *op, int len)
1679{
1680 char_u buf[10];
1681
1682 vim_strncpy(buf, op, len);
1683 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1684}
1685
1686/*
1687 * Compile code to apply '-', '+' and '!'.
1688 * When "numeric_only" is TRUE do not apply '!'.
1689 */
1690 static int
1691compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1692{
1693 char_u *p = *end;
1694
1695 // this works from end to start
1696 while (p > start)
1697 {
1698 --p;
1699 while (VIM_ISWHITE(*p))
1700 --p;
1701 if (*p == '-' || *p == '+')
1702 {
1703 int negate = *p == '-';
1704 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001705 type_T *type;
1706
Bram Moolenaar078a4612022-01-04 15:17:03 +00001707 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001708 if (type != &t_float && need_type(type, &t_number,
1709 -1, 0, cctx, FALSE, FALSE) == FAIL)
1710 return FAIL;
1711
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001712 // only '-' has an effect, for '+' we only check the type
1713 if (negate)
1714 {
1715 isn = generate_instr(cctx, ISN_NEGATENR);
1716 if (isn == NULL)
1717 return FAIL;
1718 }
1719 }
1720 else if (numeric_only)
1721 {
1722 ++p;
1723 break;
1724 }
1725 else
1726 {
1727 int invert = *p == '!';
1728
1729 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1730 {
1731 if (p[-1] == '!')
1732 invert = !invert;
1733 --p;
1734 }
1735 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1736 return FAIL;
1737 }
1738 }
1739 *end = p;
1740 return OK;
1741}
1742
1743/*
1744 * Compile "(expression)": recursive!
1745 * Return FAIL/OK.
1746 */
1747 static int
1748compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1749{
1750 int ret;
1751 char_u *p = *arg + 1;
1752
1753 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1754 return FAIL;
1755 if (ppconst->pp_used <= PPSIZE - 10)
1756 {
1757 ret = compile_expr1(arg, cctx, ppconst);
1758 }
1759 else
1760 {
1761 // Not enough space in ppconst, flush constants.
1762 if (generate_ppconst(cctx, ppconst) == FAIL)
1763 return FAIL;
1764 ret = compile_expr0(arg, cctx);
1765 }
1766 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1767 return FAIL;
1768 if (**arg == ')')
1769 ++*arg;
1770 else if (ret == OK)
1771 {
1772 emsg(_(e_missing_closing_paren));
1773 ret = FAIL;
1774 }
1775 return ret;
1776}
1777
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001778static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001779
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001780/*
1781 * Compile whatever comes after "name" or "name()".
1782 * Advances "*arg" only when something was recognized.
1783 */
1784 static int
1785compile_subscript(
1786 char_u **arg,
1787 cctx_T *cctx,
1788 char_u *start_leader,
1789 char_u **end_leader,
1790 ppconst_T *ppconst)
1791{
1792 char_u *name_start = *end_leader;
1793 int keeping_dict = FALSE;
1794
1795 for (;;)
1796 {
1797 char_u *p = skipwhite(*arg);
1798
1799 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1800 {
1801 char_u *next = peek_next_line_from_context(cctx);
1802
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001803 // If a following line starts with "->{", "->(" or "->X" advance to
1804 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001805 // Also if a following line starts with ".x".
1806 if (next != NULL &&
1807 ((next[0] == '-' && next[1] == '>'
1808 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001809 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001810 || ASCII_ISALPHA(*skipwhite(next + 2))))
1811 || (next[0] == '.' && eval_isdictc(next[1]))))
1812 {
1813 next = next_line_from_context(cctx, TRUE);
1814 if (next == NULL)
1815 return FAIL;
1816 *arg = next;
1817 p = skipwhite(*arg);
1818 }
1819 }
1820
1821 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1822 // is not a function call.
1823 if (**arg == '(')
1824 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001825 type_T *type;
1826 int argcount = 0;
1827
1828 if (generate_ppconst(cctx, ppconst) == FAIL)
1829 return FAIL;
1830 ppconst->pp_is_const = FALSE;
1831
1832 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001833 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001834
1835 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001836 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001837 return FAIL;
1838 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1839 return FAIL;
1840 if (keeping_dict)
1841 {
1842 keeping_dict = FALSE;
1843 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1844 return FAIL;
1845 }
1846 }
1847 else if (*p == '-' && p[1] == '>')
1848 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001849 char_u *pstart = p;
1850 int alt;
1851 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001852
Bram Moolenaarc7349932022-01-16 20:59:39 +00001853 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001854 if (generate_ppconst(cctx, ppconst) == FAIL)
1855 return FAIL;
1856 ppconst->pp_is_const = FALSE;
1857
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001858 // Apply the '!', '-' and '+' first:
1859 // -1.0->func() works like (-1.0)->func()
1860 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1861 return FAIL;
1862
1863 p += 2;
1864 *arg = skipwhite(p);
1865 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001866
1867 // Three alternatives handled here:
1868 // 1. "base->name(" only a name, use compile_call()
1869 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1870 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001871 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001872 alt = 2;
1873 else
1874 {
1875 // alternative 1 or 3
1876 p = *arg;
1877 if (!eval_isnamec1(*p))
1878 {
1879 semsg(_(e_trailing_characters_str), pstart);
1880 return FAIL;
1881 }
1882 if (ASCII_ISALPHA(*p) && p[1] == ':')
1883 p += 2;
1884 for ( ; eval_isnamec(*p); ++p)
1885 ;
1886 if (*p == '(')
1887 {
1888 // alternative 1
1889 alt = 1;
1890 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1891 return FAIL;
1892 }
1893 else
1894 {
1895 // Must be alternative 3, find the "(". Only works within
1896 // one line.
1897 alt = 3;
1898 paren = vim_strchr(p, '(');
1899 if (paren == NULL)
1900 {
1901 semsg(_(e_missing_parenthesis_str), *arg);
1902 return FAIL;
1903 }
1904 }
1905 }
1906
1907 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001908 {
1909 int argcount = 1;
1910 garray_T *stack = &cctx->ctx_type_stack;
1911 int type_idx_start = stack->ga_len;
1912 type_T *type;
1913 int expr_isn_start = cctx->ctx_instr.ga_len;
1914 int expr_isn_end;
1915 int arg_isn_count;
1916
Bram Moolenaarc7349932022-01-16 20:59:39 +00001917 if (alt == 2)
1918 {
1919 // Funcref call: list->(Refs[2])(arg)
1920 // or lambda: list->((arg) => expr)(arg)
1921 //
1922 // Fist compile the function expression.
1923 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1924 return FAIL;
1925 }
1926 else
1927 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001928 int fail;
1929 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001930 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001931
Bram Moolenaarc7349932022-01-16 20:59:39 +00001932 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001933
1934 // instead of using LOADG for "import.Func" use PUSHFUNC
1935 ++paren_follows_after_expr;
1936
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001937 // do not look in the next line
1938 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001939
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001940 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001941 || *skipwhite(*arg) != NUL;
1942 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001943 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001944 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001945
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001946 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001947 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001948 if (did_emsg == prev_did_emsg)
1949 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001950 return FAIL;
1951 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001952 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001953
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001954 // Compile the arguments.
1955 if (**arg != '(')
1956 {
1957 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001958 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001959 else
1960 semsg(_(e_missing_parenthesis_str), *arg);
1961 return FAIL;
1962 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001963
1964 // Remember the next instruction index, where the instructions
1965 // for arguments are being written.
1966 expr_isn_end = cctx->ctx_instr.ga_len;
1967
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001968 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001969 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
1970 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001971 return FAIL;
1972
1973 // Move the instructions for the arguments to before the
1974 // instructions of the expression and move the type of the
1975 // expression after the argument types. This is what ISN_PCALL
1976 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001977 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1978 if (arg_isn_count > 0)
1979 {
1980 int expr_isn_count = expr_isn_end - expr_isn_start;
1981 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001982 type_T *decl_type;
1983 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001984
1985 if (isn == NULL)
1986 return FAIL;
1987 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1988 + expr_isn_start,
1989 sizeof(isn_T) * expr_isn_count);
1990 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1991 + expr_isn_start,
1992 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1993 sizeof(isn_T) * arg_isn_count);
1994 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1995 + expr_isn_start + arg_isn_count,
1996 isn, sizeof(isn_T) * expr_isn_count);
1997 vim_free(isn);
1998
Bram Moolenaar078a4612022-01-04 15:17:03 +00001999 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2000 type = typep->type_curr;
2001 decl_type = typep->type_decl;
2002 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2003 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2004 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002005 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002006 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2007 typep->type_curr = type;
2008 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002009 }
2010
Bram Moolenaar078a4612022-01-04 15:17:03 +00002011 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002012 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2013 return FAIL;
2014 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002015
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002016 if (keeping_dict)
2017 {
2018 keeping_dict = FALSE;
2019 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2020 return FAIL;
2021 }
2022 }
2023 else if (**arg == '[')
2024 {
2025 int is_slice = FALSE;
2026
2027 // list index: list[123]
2028 // dict member: dict[key]
2029 // string index: text[123]
2030 // blob index: blob[123]
2031 if (generate_ppconst(cctx, ppconst) == FAIL)
2032 return FAIL;
2033 ppconst->pp_is_const = FALSE;
2034
2035 ++p;
2036 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2037 return FAIL;
2038 if (**arg == ':')
2039 {
2040 // missing first index is equal to zero
2041 generate_PUSHNR(cctx, 0);
2042 }
2043 else
2044 {
2045 if (compile_expr0(arg, cctx) == FAIL)
2046 return FAIL;
2047 if (**arg == ':')
2048 {
2049 semsg(_(e_white_space_required_before_and_after_str_at_str),
2050 ":", *arg);
2051 return FAIL;
2052 }
2053 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2054 return FAIL;
2055 *arg = skipwhite(*arg);
2056 }
2057 if (**arg == ':')
2058 {
2059 is_slice = TRUE;
2060 ++*arg;
2061 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2062 {
2063 semsg(_(e_white_space_required_before_and_after_str_at_str),
2064 ":", *arg);
2065 return FAIL;
2066 }
2067 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2068 return FAIL;
2069 if (**arg == ']')
2070 // missing second index is equal to end of string
2071 generate_PUSHNR(cctx, -1);
2072 else
2073 {
2074 if (compile_expr0(arg, cctx) == FAIL)
2075 return FAIL;
2076 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2077 return FAIL;
2078 *arg = skipwhite(*arg);
2079 }
2080 }
2081
2082 if (**arg != ']')
2083 {
2084 emsg(_(e_missing_closing_square_brace));
2085 return FAIL;
2086 }
2087 *arg = *arg + 1;
2088
2089 if (keeping_dict)
2090 {
2091 keeping_dict = FALSE;
2092 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2093 return FAIL;
2094 }
2095 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2096 return FAIL;
2097 }
2098 else if (*p == '.' && p[1] != '.')
2099 {
2100 // dictionary member: dict.name
2101 if (generate_ppconst(cctx, ppconst) == FAIL)
2102 return FAIL;
2103 ppconst->pp_is_const = FALSE;
2104
2105 *arg = p + 1;
2106 if (IS_WHITE_OR_NUL(**arg))
2107 {
2108 emsg(_(e_missing_name_after_dot));
2109 return FAIL;
2110 }
2111 p = *arg;
2112 if (eval_isdictc(*p))
2113 while (eval_isnamec(*p))
2114 MB_PTR_ADV(p);
2115 if (p == *arg)
2116 {
2117 semsg(_(e_syntax_error_at_str), *arg);
2118 return FAIL;
2119 }
2120 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2121 return FAIL;
2122 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2123 return FAIL;
2124 keeping_dict = TRUE;
2125 *arg = p;
2126 }
2127 else
2128 break;
2129 }
2130
2131 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2132 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002133 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2134 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002135 return FAIL;
2136
2137 return OK;
2138}
2139
2140/*
2141 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2142 * "arg" is advanced until after the expression, skipping white space.
2143 *
2144 * If the value is a constant "ppconst->pp_used" will be non-zero.
2145 * Before instructions are generated, any values in "ppconst" will generated.
2146 *
2147 * This is the compiling equivalent of eval1(), eval2(), etc.
2148 */
2149
2150/*
2151 * number number constant
2152 * 0zFFFFFFFF Blob constant
2153 * "string" string constant
2154 * 'string' literal string constant
2155 * &option-name option value
2156 * @r register contents
2157 * identifier variable value
2158 * function() function call
2159 * $VAR environment variable
2160 * (expression) nested expression
2161 * [expr, expr] List
2162 * {key: val, [key]: val} Dictionary
2163 *
2164 * Also handle:
2165 * ! in front logical NOT
2166 * - in front unary minus
2167 * + in front unary plus (ignored)
2168 * trailing (arg) funcref/partial call
2169 * trailing [] subscript in String or List
2170 * trailing .name entry in Dictionary
2171 * trailing ->name() method call
2172 */
2173 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002174compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002175 char_u **arg,
2176 cctx_T *cctx,
2177 ppconst_T *ppconst)
2178{
2179 char_u *start_leader, *end_leader;
2180 int ret = OK;
2181 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2182 int used_before = ppconst->pp_used;
2183
2184 ppconst->pp_is_const = FALSE;
2185
2186 /*
2187 * Skip '!', '-' and '+' characters. They are handled later.
2188 */
2189 start_leader = *arg;
2190 if (eval_leader(arg, TRUE) == FAIL)
2191 return FAIL;
2192 end_leader = *arg;
2193
2194 rettv->v_type = VAR_UNKNOWN;
2195 switch (**arg)
2196 {
2197 /*
2198 * Number constant.
2199 */
2200 case '0': // also for blob starting with 0z
2201 case '1':
2202 case '2':
2203 case '3':
2204 case '4':
2205 case '5':
2206 case '6':
2207 case '7':
2208 case '8':
2209 case '9':
2210 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2211 return FAIL;
2212 // Apply "-" and "+" just before the number now, right to
2213 // left. Matters especially when "->" follows. Stops at
2214 // '!'.
2215 if (apply_leader(rettv, TRUE,
2216 start_leader, &end_leader) == FAIL)
2217 {
2218 clear_tv(rettv);
2219 return FAIL;
2220 }
2221 break;
2222
2223 /*
2224 * String constant: "string".
2225 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002226 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002227 return FAIL;
2228 break;
2229
2230 /*
2231 * Literal string constant: 'str''ing'.
2232 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002233 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002234 return FAIL;
2235 break;
2236
2237 /*
2238 * Constant Vim variable.
2239 */
2240 case 'v': get_vim_constant(arg, rettv);
2241 ret = NOTDONE;
2242 break;
2243
2244 /*
2245 * "true" constant
2246 */
2247 case 't': if (STRNCMP(*arg, "true", 4) == 0
2248 && !eval_isnamec((*arg)[4]))
2249 {
2250 *arg += 4;
2251 rettv->v_type = VAR_BOOL;
2252 rettv->vval.v_number = VVAL_TRUE;
2253 }
2254 else
2255 ret = NOTDONE;
2256 break;
2257
2258 /*
2259 * "false" constant
2260 */
2261 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2262 && !eval_isnamec((*arg)[5]))
2263 {
2264 *arg += 5;
2265 rettv->v_type = VAR_BOOL;
2266 rettv->vval.v_number = VVAL_FALSE;
2267 }
2268 else
2269 ret = NOTDONE;
2270 break;
2271
2272 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002273 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002274 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002275 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002276 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002277 char_u *p = *arg + 4;
2278 int len;
2279
2280 for (len = 0; eval_isnamec(p[len]); ++len)
2281 ;
2282 ret = handle_predefined(*arg, len + 4, rettv);
2283 if (ret == FAIL)
2284 ret = NOTDONE;
2285 else
2286 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002287 }
2288 else
2289 ret = NOTDONE;
2290 break;
2291
2292 /*
2293 * List: [expr, expr]
2294 */
2295 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2296 return FAIL;
2297 ret = compile_list(arg, cctx, ppconst);
2298 break;
2299
2300 /*
2301 * Dictionary: {'key': val, 'key': val}
2302 */
2303 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2304 return FAIL;
2305 ret = compile_dict(arg, cctx, ppconst);
2306 break;
2307
2308 /*
2309 * Option value: &name
2310 */
2311 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2312 return FAIL;
2313 ret = compile_get_option(arg, cctx);
2314 break;
2315
2316 /*
2317 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002318 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002319 */
2320 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2321 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002322 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2323 ret = compile_interp_string(arg, cctx);
2324 else
2325 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002326 break;
2327
2328 /*
2329 * Register contents: @r.
2330 */
2331 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2332 return FAIL;
2333 ret = compile_get_register(arg, cctx);
2334 break;
2335 /*
2336 * nested expression: (expression).
2337 * lambda: (arg, arg) => expr
2338 * funcref: (arg, arg) => { statement }
2339 */
2340 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2341 ret = compile_lambda(arg, cctx);
2342 if (ret == NOTDONE)
2343 ret = compile_parenthesis(arg, cctx, ppconst);
2344 break;
2345
2346 default: ret = NOTDONE;
2347 break;
2348 }
2349 if (ret == FAIL)
2350 return FAIL;
2351
2352 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2353 {
2354 if (cctx->ctx_skip == SKIP_YES)
2355 clear_tv(rettv);
2356 else
2357 // A constant expression can possibly be handled compile time,
2358 // return the value instead of generating code.
2359 ++ppconst->pp_used;
2360 }
2361 else if (ret == NOTDONE)
2362 {
2363 char_u *p;
2364 int r;
2365
2366 if (!eval_isnamec1(**arg))
2367 {
2368 if (!vim9_bad_comment(*arg))
2369 {
2370 if (ends_excmd(*skipwhite(*arg)))
2371 semsg(_(e_empty_expression_str), *arg);
2372 else
2373 semsg(_(e_name_expected_str), *arg);
2374 }
2375 return FAIL;
2376 }
2377
2378 // "name" or "name()"
2379 p = to_name_end(*arg, TRUE);
2380 if (p - *arg == (size_t)1 && **arg == '_')
2381 {
2382 emsg(_(e_cannot_use_underscore_here));
2383 return FAIL;
2384 }
2385
2386 if (*p == '(')
2387 {
2388 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2389 }
2390 else
2391 {
2392 if (cctx->ctx_skip != SKIP_YES
2393 && generate_ppconst(cctx, ppconst) == FAIL)
2394 return FAIL;
2395 r = compile_load(arg, p, cctx, TRUE, TRUE);
2396 }
2397 if (r == FAIL)
2398 return FAIL;
2399 }
2400
2401 // Handle following "[]", ".member", etc.
2402 // Then deal with prefixed '-', '+' and '!', if not done already.
2403 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2404 ppconst) == FAIL)
2405 return FAIL;
2406 if (ppconst->pp_used > 0)
2407 {
2408 // apply the '!', '-' and '+' before the constant
2409 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2410 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2411 return FAIL;
2412 return OK;
2413 }
2414 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2415 return FAIL;
2416 return OK;
2417}
2418
2419/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002420 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002421 */
2422 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002423compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002424{
2425 type_T *want_type = NULL;
2426
2427 // Recognize <type>
2428 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2429 {
2430 ++*arg;
2431 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2432 if (want_type == NULL)
2433 return FAIL;
2434
2435 if (**arg != '>')
2436 {
2437 if (*skipwhite(*arg) == '>')
2438 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2439 else
2440 emsg(_(e_missing_gt));
2441 return FAIL;
2442 }
2443 ++*arg;
2444 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2445 return FAIL;
2446 }
2447
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002448 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002449 return FAIL;
2450
2451 if (want_type != NULL)
2452 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002453 type_T *actual;
2454 where_T where = WHERE_INIT;
2455
2456 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002457 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002458 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002459 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002460 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2461 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002462 return FAIL;
2463 }
2464 }
2465
2466 return OK;
2467}
2468
2469/*
2470 * * number multiplication
2471 * / number division
2472 * % number modulo
2473 */
2474 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002475compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002476{
2477 char_u *op;
2478 char_u *next;
2479 int ppconst_used = ppconst->pp_used;
2480
2481 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002482 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002483 return FAIL;
2484
2485 /*
2486 * Repeat computing, until no "*", "/" or "%" is following.
2487 */
2488 for (;;)
2489 {
2490 op = may_peek_next_line(cctx, *arg, &next);
2491 if (*op != '*' && *op != '/' && *op != '%')
2492 break;
2493 if (next != NULL)
2494 {
2495 *arg = next_line_from_context(cctx, TRUE);
2496 op = skipwhite(*arg);
2497 }
2498
2499 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2500 {
2501 error_white_both(op, 1);
2502 return FAIL;
2503 }
2504 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2505 return FAIL;
2506
2507 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002508 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002509 return FAIL;
2510
2511 if (ppconst->pp_used == ppconst_used + 2
2512 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2513 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2514 {
2515 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2516 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2517 varnumber_T res = 0;
2518 int failed = FALSE;
2519
2520 // both are numbers: compute the result
2521 switch (*op)
2522 {
2523 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2524 break;
2525 case '/': res = num_divide(tv1->vval.v_number,
2526 tv2->vval.v_number, &failed);
2527 break;
2528 case '%': res = num_modulus(tv1->vval.v_number,
2529 tv2->vval.v_number, &failed);
2530 break;
2531 }
2532 if (failed)
2533 return FAIL;
2534 tv1->vval.v_number = res;
2535 --ppconst->pp_used;
2536 }
2537 else
2538 {
2539 generate_ppconst(cctx, ppconst);
2540 generate_two_op(cctx, op);
2541 }
2542 }
2543
2544 return OK;
2545}
2546
2547/*
2548 * + number addition or list/blobl concatenation
2549 * - number subtraction
2550 * .. string concatenation
2551 */
2552 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002553compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002554{
2555 char_u *op;
2556 char_u *next;
2557 int oplen;
2558 int ppconst_used = ppconst->pp_used;
2559
2560 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002561 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002562 return FAIL;
2563
2564 /*
2565 * Repeat computing, until no "+", "-" or ".." is following.
2566 */
2567 for (;;)
2568 {
2569 op = may_peek_next_line(cctx, *arg, &next);
2570 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2571 break;
2572 if (op[0] == op[1] && *op != '.' && next)
2573 // Finding "++" or "--" on the next line is a separate command.
2574 // But ".." is concatenation.
2575 break;
2576 oplen = (*op == '.' ? 2 : 1);
2577 if (next != NULL)
2578 {
2579 *arg = next_line_from_context(cctx, TRUE);
2580 op = skipwhite(*arg);
2581 }
2582
2583 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2584 {
2585 error_white_both(op, oplen);
2586 return FAIL;
2587 }
2588
2589 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2590 return FAIL;
2591
2592 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002593 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002594 return FAIL;
2595
2596 if (ppconst->pp_used == ppconst_used + 2
2597 && (*op == '.'
2598 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2599 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2600 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2601 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2602 {
2603 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2604 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2605
2606 // concat/subtract/add constant numbers
2607 if (*op == '+')
2608 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2609 else if (*op == '-')
2610 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2611 else
2612 {
2613 // concatenate constant strings
2614 char_u *s1 = tv1->vval.v_string;
2615 char_u *s2 = tv2->vval.v_string;
2616 size_t len1 = STRLEN(s1);
2617
2618 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2619 if (tv1->vval.v_string == NULL)
2620 {
2621 clear_ppconst(ppconst);
2622 return FAIL;
2623 }
2624 mch_memmove(tv1->vval.v_string, s1, len1);
2625 STRCPY(tv1->vval.v_string + len1, s2);
2626 vim_free(s1);
2627 vim_free(s2);
2628 }
2629 --ppconst->pp_used;
2630 }
2631 else
2632 {
2633 generate_ppconst(cctx, ppconst);
2634 ppconst->pp_is_const = FALSE;
2635 if (*op == '.')
2636 {
2637 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2638 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2639 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002640 if (generate_CONCAT(cctx, 2) == FAIL)
2641 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002642 }
2643 else
2644 generate_two_op(cctx, op);
2645 }
2646 }
2647
2648 return OK;
2649}
2650
2651/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002652 * expr6a >> expr6b
2653 * expr6a << expr6b
2654 *
2655 * Produces instructions:
2656 * OPNR bitwise left or right shift
2657 */
2658 static int
2659compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2660{
2661 exprtype_T type = EXPR_UNKNOWN;
2662 char_u *p;
2663 char_u *next;
2664 int len = 2;
2665 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002666 isn_T *isn;
2667
2668 // get the first variable
2669 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2670 return FAIL;
2671
2672 /*
2673 * Repeat computing, until no "+", "-" or ".." is following.
2674 */
2675 for (;;)
2676 {
2677 type = EXPR_UNKNOWN;
2678
2679 p = may_peek_next_line(cctx, *arg, &next);
2680 if (p[0] == '<' && p[1] == '<')
2681 type = EXPR_LSHIFT;
2682 else if (p[0] == '>' && p[1] == '>')
2683 type = EXPR_RSHIFT;
2684
2685 if (type == EXPR_UNKNOWN)
2686 return OK;
2687
2688 // Handle a bitwise left or right shift operator
2689 if (ppconst->pp_used == ppconst_used + 1)
2690 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002691 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002692 {
2693 // left operand should be a number
2694 emsg(_(e_bitshift_ops_must_be_number));
2695 return FAIL;
2696 }
2697 }
2698 else
2699 {
2700 type_T *t = get_type_on_stack(cctx, 0);
2701
2702 if (need_type(t, &t_number, 0, 0, cctx, FALSE, FALSE) == FAIL)
2703 {
2704 emsg(_(e_bitshift_ops_must_be_number));
2705 return FAIL;
2706 }
2707 }
2708
2709 if (next != NULL)
2710 {
2711 *arg = next_line_from_context(cctx, TRUE);
2712 p = skipwhite(*arg);
2713 }
2714
2715 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2716 {
2717 error_white_both(p, len);
2718 return FAIL;
2719 }
2720
2721 // get the second variable
2722 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2723 return FAIL;
2724
2725 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2726 return FAIL;
2727
2728 if (ppconst->pp_used == ppconst_used + 2)
2729 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002730 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2731 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2732
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002733 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002734 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2735 {
2736 // right operand should be a positive number
2737 if (tv2->v_type != VAR_NUMBER)
2738 emsg(_(e_bitshift_ops_must_be_number));
2739 else
dundargocc57b5bc2022-11-02 13:30:51 +00002740 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002741 return FAIL;
2742 }
2743
2744 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2745 tv1->vval.v_number = 0;
2746 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002747 tv1->vval.v_number =
2748 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002749 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002750 tv1->vval.v_number =
2751 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002752 clear_tv(tv2);
2753 --ppconst->pp_used;
2754 }
2755 else
2756 {
2757 if (need_type(get_type_on_stack(cctx, 0), &t_number, 0, 0, cctx,
2758 FALSE, FALSE) == FAIL)
2759 {
2760 emsg(_(e_bitshift_ops_must_be_number));
2761 return FAIL;
2762 }
2763
2764 generate_ppconst(cctx, ppconst);
2765
2766 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2767 if (isn == NULL)
2768 return FAIL;
2769
2770 if (isn != NULL)
2771 isn->isn_arg.op.op_type = type;
2772 }
2773 }
2774
2775 return OK;
2776}
2777
2778/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002779 * expr5a == expr5b
2780 * expr5a =~ expr5b
2781 * expr5a != expr5b
2782 * expr5a !~ expr5b
2783 * expr5a > expr5b
2784 * expr5a >= expr5b
2785 * expr5a < expr5b
2786 * expr5a <= expr5b
2787 * expr5a is expr5b
2788 * expr5a isnot expr5b
2789 *
2790 * Produces instructions:
2791 * EVAL expr5a Push result of "expr5a"
2792 * EVAL expr5b Push result of "expr5b"
2793 * COMPARE one of the compare instructions
2794 */
2795 static int
2796compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2797{
2798 exprtype_T type = EXPR_UNKNOWN;
2799 char_u *p;
2800 char_u *next;
2801 int len = 2;
2802 int type_is = FALSE;
2803 int ppconst_used = ppconst->pp_used;
2804
2805 // get the first variable
2806 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2807 return FAIL;
2808
2809 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002810
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002811 type = get_compare_type(p, &len, &type_is);
2812
2813 /*
2814 * If there is a comparative operator, use it.
2815 */
2816 if (type != EXPR_UNKNOWN)
2817 {
2818 int ic = FALSE; // Default: do not ignore case
2819
2820 if (next != NULL)
2821 {
2822 *arg = next_line_from_context(cctx, TRUE);
2823 p = skipwhite(*arg);
2824 }
2825 if (type_is && (p[len] == '?' || p[len] == '#'))
2826 {
2827 semsg(_(e_invalid_expression_str), *arg);
2828 return FAIL;
2829 }
2830 // extra question mark appended: ignore case
2831 if (p[len] == '?')
2832 {
2833 ic = TRUE;
2834 ++len;
2835 }
2836 // extra '#' appended: match case (ignored)
2837 else if (p[len] == '#')
2838 ++len;
2839 // nothing appended: match case
2840
2841 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2842 {
2843 error_white_both(p, len);
2844 return FAIL;
2845 }
2846
2847 // get the second variable
2848 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2849 return FAIL;
2850
2851 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2852 return FAIL;
2853
2854 if (ppconst->pp_used == ppconst_used + 2)
2855 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002856 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002857 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2858 int ret;
2859
2860 // Both sides are a constant, compute the result now.
2861 // First check for a valid combination of types, this is more
2862 // strict than typval_compare().
2863 if (check_compare_types(type, tv1, tv2) == FAIL)
2864 ret = FAIL;
2865 else
2866 {
2867 ret = typval_compare(tv1, tv2, type, ic);
2868 tv1->v_type = VAR_BOOL;
2869 tv1->vval.v_number = tv1->vval.v_number
2870 ? VVAL_TRUE : VVAL_FALSE;
2871 clear_tv(tv2);
2872 --ppconst->pp_used;
2873 }
2874 return ret;
2875 }
2876
2877 generate_ppconst(cctx, ppconst);
2878 return generate_COMPARE(cctx, type, ic);
2879 }
2880
2881 return OK;
2882}
2883
2884static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2885
2886/*
2887 * Compile || or &&.
2888 */
2889 static int
2890compile_and_or(
2891 char_u **arg,
2892 cctx_T *cctx,
2893 char *op,
2894 ppconst_T *ppconst,
2895 int ppconst_used UNUSED)
2896{
2897 char_u *next;
2898 char_u *p = may_peek_next_line(cctx, *arg, &next);
2899 int opchar = *op;
2900
2901 if (p[0] == opchar && p[1] == opchar)
2902 {
2903 garray_T *instr = &cctx->ctx_instr;
2904 garray_T end_ga;
2905 int save_skip = cctx->ctx_skip;
2906
2907 /*
2908 * Repeat until there is no following "||" or "&&"
2909 */
2910 ga_init2(&end_ga, sizeof(int), 10);
2911 while (p[0] == opchar && p[1] == opchar)
2912 {
2913 long start_lnum = SOURCING_LNUM;
2914 long save_sourcing_lnum;
2915 int start_ctx_lnum = cctx->ctx_lnum;
2916 int save_lnum;
2917 int const_used;
2918 int status;
2919 jumpwhen_T jump_when = opchar == '|'
2920 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2921
2922 if (next != NULL)
2923 {
2924 *arg = next_line_from_context(cctx, TRUE);
2925 p = skipwhite(*arg);
2926 }
2927
2928 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2929 {
2930 semsg(_(e_white_space_required_before_and_after_str_at_str),
2931 op, p);
2932 ga_clear(&end_ga);
2933 return FAIL;
2934 }
2935
2936 save_sourcing_lnum = SOURCING_LNUM;
2937 SOURCING_LNUM = start_lnum;
2938 save_lnum = cctx->ctx_lnum;
2939 cctx->ctx_lnum = start_ctx_lnum;
2940
2941 status = check_ppconst_bool(ppconst);
2942 if (status != FAIL)
2943 {
2944 // Use the last ppconst if possible.
2945 if (ppconst->pp_used > 0)
2946 {
2947 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2948 int is_true = tv2bool(tv);
2949
2950 if ((is_true && opchar == '|')
2951 || (!is_true && opchar == '&'))
2952 {
2953 // For "false && expr" and "true || expr" the "expr"
2954 // does not need to be evaluated.
2955 cctx->ctx_skip = SKIP_YES;
2956 clear_tv(tv);
2957 tv->v_type = VAR_BOOL;
2958 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2959 }
2960 else
2961 {
2962 // For "true && expr" and "false || expr" only "expr"
2963 // needs to be evaluated.
2964 --ppconst->pp_used;
2965 jump_when = JUMP_NEVER;
2966 }
2967 }
2968 else
2969 {
2970 // Every part must evaluate to a bool.
2971 status = bool_on_stack(cctx);
2972 }
2973 }
2974 if (status != FAIL)
2975 status = ga_grow(&end_ga, 1);
2976 cctx->ctx_lnum = save_lnum;
2977 if (status == FAIL)
2978 {
2979 ga_clear(&end_ga);
2980 return FAIL;
2981 }
2982
2983 if (jump_when != JUMP_NEVER)
2984 {
2985 if (cctx->ctx_skip != SKIP_YES)
2986 {
2987 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2988 ++end_ga.ga_len;
2989 }
2990 generate_JUMP(cctx, jump_when, 0);
2991 }
2992
2993 // eval the next expression
2994 SOURCING_LNUM = save_sourcing_lnum;
2995 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2996 {
2997 ga_clear(&end_ga);
2998 return FAIL;
2999 }
3000
3001 const_used = ppconst->pp_used;
3002 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3003 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3004 {
3005 ga_clear(&end_ga);
3006 return FAIL;
3007 }
3008
3009 // "0 || 1" results in true, "1 && 0" results in false.
3010 if (ppconst->pp_used == const_used + 1)
3011 {
3012 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3013
3014 if (tv->v_type == VAR_NUMBER
3015 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3016 {
3017 tv->vval.v_number = tv->vval.v_number == 1
3018 ? VVAL_TRUE : VVAL_FALSE;
3019 tv->v_type = VAR_BOOL;
3020 }
3021 }
3022
3023 p = may_peek_next_line(cctx, *arg, &next);
3024 }
3025
3026 if (check_ppconst_bool(ppconst) == FAIL)
3027 {
3028 ga_clear(&end_ga);
3029 return FAIL;
3030 }
3031
3032 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3033 // Every part must evaluate to a bool.
3034 if (bool_on_stack(cctx) == FAIL)
3035 {
3036 ga_clear(&end_ga);
3037 return FAIL;
3038 }
3039
3040 if (end_ga.ga_len > 0)
3041 {
3042 // Fill in the end label in all jumps.
3043 generate_ppconst(cctx, ppconst);
3044 while (end_ga.ga_len > 0)
3045 {
3046 isn_T *isn;
3047
3048 --end_ga.ga_len;
3049 isn = ((isn_T *)instr->ga_data)
3050 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3051 isn->isn_arg.jump.jump_where = instr->ga_len;
3052 }
3053 }
3054 ga_clear(&end_ga);
3055
3056 cctx->ctx_skip = save_skip;
3057 }
3058
3059 return OK;
3060}
3061
3062/*
3063 * expr4a && expr4a && expr4a logical AND
3064 *
3065 * Produces instructions:
3066 * EVAL expr4a Push result of "expr4a"
3067 * COND2BOOL convert to bool if needed
3068 * JUMP_IF_COND_FALSE end
3069 * EVAL expr4b Push result of "expr4b"
3070 * JUMP_IF_COND_FALSE end
3071 * EVAL expr4c Push result of "expr4c"
3072 * end:
3073 */
3074 static int
3075compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3076{
3077 int ppconst_used = ppconst->pp_used;
3078
3079 // get the first variable
3080 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3081 return FAIL;
3082
3083 // || and && work almost the same
3084 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3085}
3086
3087/*
3088 * expr3a || expr3b || expr3c logical OR
3089 *
3090 * Produces instructions:
3091 * EVAL expr3a Push result of "expr3a"
3092 * COND2BOOL convert to bool if needed
3093 * JUMP_IF_COND_TRUE end
3094 * EVAL expr3b Push result of "expr3b"
3095 * JUMP_IF_COND_TRUE end
3096 * EVAL expr3c Push result of "expr3c"
3097 * end:
3098 */
3099 static int
3100compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3101{
3102 int ppconst_used = ppconst->pp_used;
3103
3104 // eval the first expression
3105 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3106 return FAIL;
3107
3108 // || and && work almost the same
3109 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3110}
3111
3112/*
3113 * Toplevel expression: expr2 ? expr1a : expr1b
3114 * Produces instructions:
3115 * EVAL expr2 Push result of "expr2"
3116 * JUMP_IF_FALSE alt jump if false
3117 * EVAL expr1a
3118 * JUMP_ALWAYS end
3119 * alt: EVAL expr1b
3120 * end:
3121 *
3122 * Toplevel expression: expr2 ?? expr1
3123 * Produces instructions:
3124 * EVAL expr2 Push result of "expr2"
3125 * JUMP_AND_KEEP_IF_TRUE end jump if true
3126 * EVAL expr1
3127 * end:
3128 */
3129 int
3130compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3131{
3132 char_u *p;
3133 int ppconst_used = ppconst->pp_used;
3134 char_u *next;
3135
3136 // Ignore all kinds of errors when not producing code.
3137 if (cctx->ctx_skip == SKIP_YES)
3138 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003139 int prev_did_emsg = did_emsg;
3140
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003141 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003142 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003143 }
3144
3145 // Evaluate the first expression.
3146 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3147 return FAIL;
3148
3149 p = may_peek_next_line(cctx, *arg, &next);
3150 if (*p == '?')
3151 {
3152 int op_falsy = p[1] == '?';
3153 garray_T *instr = &cctx->ctx_instr;
3154 garray_T *stack = &cctx->ctx_type_stack;
3155 int alt_idx = instr->ga_len;
3156 int end_idx = 0;
3157 isn_T *isn;
3158 type_T *type1 = NULL;
3159 int has_const_expr = FALSE;
3160 int const_value = FALSE;
3161 int save_skip = cctx->ctx_skip;
3162
3163 if (next != NULL)
3164 {
3165 *arg = next_line_from_context(cctx, TRUE);
3166 p = skipwhite(*arg);
3167 }
3168
3169 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3170 {
3171 semsg(_(e_white_space_required_before_and_after_str_at_str),
3172 op_falsy ? "??" : "?", p);
3173 return FAIL;
3174 }
3175
3176 if (ppconst->pp_used == ppconst_used + 1)
3177 {
3178 // the condition is a constant, we know whether the ? or the :
3179 // expression is to be evaluated.
3180 has_const_expr = TRUE;
3181 if (op_falsy)
3182 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3183 else
3184 {
3185 int error = FALSE;
3186
3187 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3188 &error);
3189 if (error)
3190 return FAIL;
3191 }
3192 cctx->ctx_skip = save_skip == SKIP_YES ||
3193 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3194
3195 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3196 // "left ?? right" and "left" is truthy: produce "left"
3197 generate_ppconst(cctx, ppconst);
3198 else
3199 {
3200 clear_tv(&ppconst->pp_tv[ppconst_used]);
3201 --ppconst->pp_used;
3202 }
3203 }
3204 else
3205 {
3206 generate_ppconst(cctx, ppconst);
3207 if (op_falsy)
3208 end_idx = instr->ga_len;
3209 generate_JUMP(cctx, op_falsy
3210 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3211 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003212 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003213 }
3214
3215 // evaluate the second expression; any type is accepted
3216 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3217 return FAIL;
3218 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3219 return FAIL;
3220
3221 if (!has_const_expr)
3222 {
3223 generate_ppconst(cctx, ppconst);
3224
3225 if (!op_falsy)
3226 {
3227 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003228 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003229 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003230
3231 end_idx = instr->ga_len;
3232 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3233
3234 // jump here from JUMP_IF_FALSE
3235 isn = ((isn_T *)instr->ga_data) + alt_idx;
3236 isn->isn_arg.jump.jump_where = instr->ga_len;
3237 }
3238 }
3239
3240 if (!op_falsy)
3241 {
3242 // Check for the ":".
3243 p = may_peek_next_line(cctx, *arg, &next);
3244 if (*p != ':')
3245 {
3246 emsg(_(e_missing_colon_after_questionmark));
3247 return FAIL;
3248 }
3249 if (next != NULL)
3250 {
3251 *arg = next_line_from_context(cctx, TRUE);
3252 p = skipwhite(*arg);
3253 }
3254
3255 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3256 {
3257 semsg(_(e_white_space_required_before_and_after_str_at_str),
3258 ":", p);
3259 return FAIL;
3260 }
3261
3262 // evaluate the third expression
3263 if (has_const_expr)
3264 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3265 ? SKIP_YES : SKIP_NOT;
3266 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3267 return FAIL;
3268 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3269 return FAIL;
3270 }
3271
3272 if (!has_const_expr)
3273 {
3274 type_T **typep;
3275
3276 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003277 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003278
3279 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003280 typep = &((((type2_T *)stack->ga_data)
3281 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003282 common_type(type1, *typep, typep, cctx->ctx_type_list);
3283
3284 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3285 isn = ((isn_T *)instr->ga_data) + end_idx;
3286 isn->isn_arg.jump.jump_where = instr->ga_len;
3287 }
3288
3289 cctx->ctx_skip = save_skip;
3290 }
3291 return OK;
3292}
3293
3294/*
3295 * Toplevel expression.
3296 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3297 * Returns OK or FAIL.
3298 */
3299 int
3300compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3301{
3302 ppconst_T ppconst;
3303
3304 CLEAR_FIELD(ppconst);
3305 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3306 {
3307 clear_ppconst(&ppconst);
3308 return FAIL;
3309 }
3310 if (is_const != NULL)
3311 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3312 if (generate_ppconst(cctx, &ppconst) == FAIL)
3313 return FAIL;
3314 return OK;
3315}
3316
3317/*
3318 * Toplevel expression.
3319 */
3320 int
3321compile_expr0(char_u **arg, cctx_T *cctx)
3322{
3323 return compile_expr0_ext(arg, cctx, NULL);
3324}
3325
3326
3327#endif // defined(FEAT_EVAL)