blob: 0e2e8eac9423f14bbdfe0038757430057a3c4f3e [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:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000238 case VAR_CLASS:
239 case VAR_OBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000240 case VAR_UNKNOWN:
241 case VAR_ANY:
242 case VAR_VOID:
243 emsg(_(e_cannot_index_special_variable));
244 break;
245 default:
246 emsg(_(e_string_list_dict_or_blob_required));
247 }
248 return FAIL;
249 }
250 return OK;
251}
252
253/*
254 * Generate an instruction to load script-local variable "name", without the
255 * leading "s:".
256 * Also finds imported variables.
257 */
258 int
259compile_load_scriptvar(
260 cctx_T *cctx,
261 char_u *name, // variable NUL terminated
262 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100263 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000264{
265 scriptitem_T *si;
266 int idx;
267 imported_T *import;
268
269 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
270 return FAIL;
271 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000272 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000273 if (idx >= 0)
274 {
275 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
276
277 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
278 current_sctx.sc_sid, idx, sv->sv_type);
279 return OK;
280 }
281
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000282 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000283 if (import != NULL)
284 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000285 char_u *p = skipwhite(*end);
286 char_u *exp_name;
287 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100288 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000289 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000290 int done = FALSE;
291 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000292
293 // Need to lookup the member.
294 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000295 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000296 semsg(_(e_expected_dot_after_name_str), start);
297 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000298 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000299 ++p;
300 if (VIM_ISWHITE(*p))
301 {
302 emsg(_(e_no_white_space_allowed_after_dot));
303 return FAIL;
304 }
305
306 // isolate one name
307 exp_name = p;
308 while (eval_isnamec(*p))
309 ++p;
310 cc = *p;
311 *p = NUL;
312
Bram Moolenaard041f422022-01-12 19:54:00 +0000313 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100314 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
315 // "import autoload './dir/script.vim'" or
316 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100317 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100318
319 if (res == OK)
320 {
321 if (si->sn_autoload_prefix != NULL
322 && si->sn_state == SN_STATE_NOT_LOADED)
323 {
324 char_u *auto_name =
325 concat_str(si->sn_autoload_prefix, exp_name);
326
327 // autoload script must be loaded later, access by the autoload
328 // name. If a '(' follows it must be a function. Otherwise we
329 // don't know, it can be "script.Func".
330 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100331 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100332 else
333 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
334 vim_free(auto_name);
335 done = TRUE;
336 }
337 else if (si->sn_import_autoload
338 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100339 {
340 // If a '(' follows it must be a function. Otherwise we don't
341 // know, it can be "script.Func".
342 if (cc == '(' || paren_follows_after_expr)
343 {
344 char_u sid_name[MAX_FUNC_NAME_LEN];
345
346 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100347 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100348 }
349 else
350 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
351 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100352 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100353 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100354 else
355 {
356 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
357 cctx, NULL, TRUE);
358 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100359 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100360
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000361 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000362 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000363 if (done)
364 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000365
366 if (idx < 0)
367 {
368 if (ufunc != NULL)
369 {
370 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100371 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000372 return OK;
373 }
374 return FAIL;
375 }
376
377 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
378 import->imp_sid,
379 idx,
380 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000381 return OK;
382 }
383
Bram Moolenaard0132f42022-05-12 11:05:40 +0100384 // Can only get here if we know "name" is a script variable and not in a
385 // Vim9 script (variable is not in sn_var_vals): old style script.
386 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000387 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000388}
389
390 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000391generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000392{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000393 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000394 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000395
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000396 // Reject a global non-autoload function found without the "g:" prefix.
397 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000398 return FAIL;
399
400 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000401 compile_type = get_compile_type(ufunc);
402 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000403 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000404 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100405 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000406}
407
408/*
409 * Compile a variable name into a load instruction.
410 * "end" points to just after the name.
411 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
412 * When "error" is FALSE do not give an error when not found.
413 */
414 int
415compile_load(
416 char_u **arg,
417 char_u *end_arg,
418 cctx_T *cctx,
419 int is_expr,
420 int error)
421{
422 type_T *type;
423 char_u *name = NULL;
424 char_u *end = end_arg;
425 int res = FAIL;
426 int prev_called_emsg = called_emsg;
427
428 if (*(*arg + 1) == ':')
429 {
430 if (end <= *arg + 2)
431 {
432 isntype_T isn_type;
433
434 // load dictionary of namespace
435 switch (**arg)
436 {
437 case 'g': isn_type = ISN_LOADGDICT; break;
438 case 'w': isn_type = ISN_LOADWDICT; break;
439 case 't': isn_type = ISN_LOADTDICT; break;
440 case 'b': isn_type = ISN_LOADBDICT; break;
441 default:
442 semsg(_(e_namespace_not_supported_str), *arg);
443 goto theend;
444 }
445 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
446 goto theend;
447 res = OK;
448 }
449 else
450 {
451 isntype_T isn_type = ISN_DROP;
452
453 // load namespaced variable
454 name = vim_strnsave(*arg + 2, end - (*arg + 2));
455 if (name == NULL)
456 return FAIL;
457
458 switch (**arg)
459 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100460 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000461 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000462 case 's': if (current_script_is_vim9())
463 {
464 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
465 *arg);
466 vim_free(name);
467 return FAIL;
468 }
Kota Kato948a3892022-08-16 16:09:59 +0100469 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000470 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000471 else
472 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100473 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000474 break;
475 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
476 {
477 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000478 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000479 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000480 else
481 isn_type = ISN_LOADG;
482 }
483 else
484 {
485 isn_type = ISN_LOADAUTO;
486 vim_free(name);
487 name = vim_strnsave(*arg, end - *arg);
488 if (name == NULL)
489 return FAIL;
490 }
491 break;
492 case 'w': isn_type = ISN_LOADW; break;
493 case 't': isn_type = ISN_LOADT; break;
494 case 'b': isn_type = ISN_LOADB; break;
495 default: // cannot happen, just in case
496 semsg(_(e_namespace_not_supported_str), *arg);
497 goto theend;
498 }
499 if (isn_type != ISN_DROP)
500 {
501 // Global, Buffer-local, Window-local and Tabpage-local
502 // variables can be defined later, thus we don't check if it
503 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000504 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000505 }
506 }
507 }
508 else
509 {
510 size_t len = end - *arg;
511 int idx;
512 int gen_load = FALSE;
513 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100514 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100515 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000516
517 name = vim_strnsave(*arg, end - *arg);
518 if (name == NULL)
519 return FAIL;
520
521 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
522 {
523 script_autoload(name, FALSE);
524 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
525 }
526 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
527 == OK)
528 {
529 if (gen_load_outer == 0)
530 gen_load = TRUE;
531 }
532 else
533 {
534 lvar_T lvar;
535
536 if (lookup_local(*arg, len, &lvar, cctx) == OK)
537 {
538 type = lvar.lv_type;
539 idx = lvar.lv_idx;
540 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100541 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000542 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100543 outer_loop_depth = lvar.lv_loop_depth;
544 outer_loop_idx = lvar.lv_loop_idx;
545 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000546 else
547 gen_load = TRUE;
548 }
549 else
550 {
551 // "var" can be script-local even without using "s:" if it
552 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000553 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000554 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100555 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000556
557 // When evaluating an expression and the name starts with an
558 // uppercase letter it can be a user defined function.
559 // generate_funcref() will fail if the function can't be found.
560 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000561 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000562 }
563 }
564 if (gen_load)
565 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
566 if (gen_load_outer > 0)
567 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100568 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
569 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000570 cctx->ctx_outer_used = TRUE;
571 }
572 }
573
574 *arg = end;
575
576theend:
577 if (res == FAIL && error && called_emsg == prev_called_emsg)
578 semsg(_(e_variable_not_found_str), name);
579 vim_free(name);
580 return res;
581}
582
583/*
584 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100585 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000586 * Returns FAIL if compilation fails.
587 */
588 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100589compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000590{
LemonBoyf3b48952022-05-05 13:53:03 +0100591 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000592 garray_T save_ga = cctx->ctx_instr;
593 int expr_res;
594 int trailing_error;
595 int instr_count;
596 isn_T *instr = NULL;
597
598 // Remove the string type from the stack.
599 --cctx->ctx_type_stack.ga_len;
600
601 // Temporarily reset the list of instructions so that the jump labels are
602 // correct.
603 cctx->ctx_instr.ga_len = 0;
604 cctx->ctx_instr.ga_maxlen = 0;
605 cctx->ctx_instr.ga_data = NULL;
606 expr_res = compile_expr0(&s, cctx);
607 s = skipwhite(s);
608 trailing_error = *s != NUL;
609
610 if (expr_res == FAIL || trailing_error
611 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
612 {
613 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000614 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000615 clear_instr_ga(&cctx->ctx_instr);
616 cctx->ctx_instr = save_ga;
617 ++cctx->ctx_type_stack.ga_len;
618 return FAIL;
619 }
620
621 // Move the generated instructions into the ISN_INSTR instruction, then
622 // restore the list of instructions.
623 instr_count = cctx->ctx_instr.ga_len;
624 instr = cctx->ctx_instr.ga_data;
625 instr[instr_count].isn_type = ISN_FINISH;
626
627 cctx->ctx_instr = save_ga;
628 vim_free(isn->isn_arg.string);
629 isn->isn_type = ISN_INSTR;
630 isn->isn_arg.instr = instr;
631 return OK;
632}
633
634/*
635 * Compile the argument expressions.
636 * "arg" points to just after the "(" and is advanced to after the ")"
637 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100638 int
LemonBoyf3b48952022-05-05 13:53:03 +0100639compile_arguments(
640 char_u **arg,
641 cctx_T *cctx,
642 int *argcount,
643 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000644{
645 char_u *p = *arg;
646 char_u *whitep = *arg;
647 int must_end = FALSE;
648 int instr_count;
649
650 for (;;)
651 {
652 if (may_get_next_line(whitep, &p, cctx) == FAIL)
653 goto failret;
654 if (*p == ')')
655 {
656 *arg = p + 1;
657 return OK;
658 }
659 if (must_end)
660 {
661 semsg(_(e_missing_comma_before_argument_str), p);
662 return FAIL;
663 }
664
665 instr_count = cctx->ctx_instr.ga_len;
666 if (compile_expr0(&p, cctx) == FAIL)
667 return FAIL;
668 ++*argcount;
669
LemonBoyf3b48952022-05-05 13:53:03 +0100670 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000671 && cctx->ctx_instr.ga_len == instr_count + 1)
672 {
673 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
674
675 // {skip} argument of searchpair() can be compiled if not empty
676 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100677 compile_string(isn, cctx, 0);
678 }
679 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
680 && cctx->ctx_instr.ga_len == instr_count + 1)
681 {
682 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
683
684 // {sub} argument of substitute() can be compiled if it starts
685 // with \=
686 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100687 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100688 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000689 }
690
691 if (*p != ',' && *skipwhite(p) == ',')
692 {
693 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
694 p = skipwhite(p);
695 }
696 if (*p == ',')
697 {
698 ++p;
699 if (*p != NUL && !VIM_ISWHITE(*p))
700 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
701 }
702 else
703 must_end = TRUE;
704 whitep = p;
705 p = skipwhite(p);
706 }
707failret:
708 emsg(_(e_missing_closing_paren));
709 return FAIL;
710}
711
712/*
713 * Compile a function call: name(arg1, arg2)
714 * "arg" points to "name", "arg + varlen" to the "(".
715 * "argcount_init" is 1 for "value->method()"
716 * Instructions:
717 * EVAL arg1
718 * EVAL arg2
719 * BCALL / DCALL / UCALL
720 */
721 static int
722compile_call(
723 char_u **arg,
724 size_t varlen,
725 cctx_T *cctx,
726 ppconst_T *ppconst,
727 int argcount_init)
728{
729 char_u *name = *arg;
730 char_u *p;
731 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100732 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000733 char_u fname_buf[FLEN_FIXED + 1];
734 char_u *tofree = NULL;
735 int error = FCERR_NONE;
736 ufunc_T *ufunc = NULL;
737 int res = FAIL;
738 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000739 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100740 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000741 imported_T *import;
742
743 if (varlen >= sizeof(namebuf))
744 {
745 semsg(_(e_name_too_long_str), name);
746 return FAIL;
747 }
748 vim_strncpy(namebuf, *arg, varlen);
749
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000750 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000751 if (import != NULL)
752 {
753 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
754 return FAIL;
755 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000756
757 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100758 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000759 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100760 if ((varlen == 3
761 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000762 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
763 {
764 char_u *s = skipwhite(*arg + varlen + 1);
765 typval_T argvars[2];
766 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100767 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000768
769 argvars[0].v_type = VAR_UNKNOWN;
770 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100771 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100773 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000774 s = skipwhite(s);
775 if (*s == ')' && argvars[0].v_type == VAR_STRING
776 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
777 || !is_has))
778 {
779 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
780
781 *arg = s + 1;
782 argvars[1].v_type = VAR_UNKNOWN;
783 tv->v_type = VAR_NUMBER;
784 tv->vval.v_number = 0;
785 if (is_has)
786 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100787 else if (is_len)
788 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000789 else
790 f_exists(argvars, tv);
791 clear_tv(&argvars[0]);
792 ++ppconst->pp_used;
793 return OK;
794 }
795 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100796 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000797 {
798 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
799 return FAIL;
800 }
801 }
802
803 if (generate_ppconst(cctx, ppconst) == FAIL)
804 return FAIL;
805
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000806 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
807
808 // We handle the "skip" argument of searchpair() and searchpairpos()
809 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100810 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
811 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
812 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
813 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
814 special_fn = CA_SEARCHPAIR;
815 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
816 special_fn = CA_SUBSTITUTE;
817 else
818 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000819
820 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100821 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000822 goto theend;
823
824 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
825 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
826 {
827 int idx;
828
829 // builtin function
830 idx = find_internal_func(name);
831 if (idx >= 0)
832 {
833 if (STRCMP(name, "flatten") == 0)
834 {
835 emsg(_(e_cannot_use_flatten_in_vim9_script));
836 goto theend;
837 }
838
839 if (STRCMP(name, "add") == 0 && argcount == 2)
840 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000841 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000842
843 // add() can be compiled to instructions if we know the type
844 if (type->tt_type == VAR_LIST)
845 {
846 // inline "add(list, item)" so that the type can be checked
847 res = generate_LISTAPPEND(cctx);
848 idx = -1;
849 }
850 else if (type->tt_type == VAR_BLOB)
851 {
852 // inline "add(blob, nr)" so that the type can be checked
853 res = generate_BLOBAPPEND(cctx);
854 idx = -1;
855 }
856 }
857
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100858 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
859 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100860 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100861 // May have the "D" or "R" flag, reserve a variable for a
862 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100863 if (get_defer_var_idx(cctx) == 0)
864 idx = -1;
865 }
866
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000867 if (idx >= 0)
868 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
869 }
870 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100871 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000872 goto theend;
873 }
874
Bram Moolenaar62aec932022-01-29 21:45:34 +0000875 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
876
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000877 // An argument or local variable can be a function reference, this
878 // overrules a function name.
879 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
880 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
881 {
882 // If we can find the function by name generate the right call.
883 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000884 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000885 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000886 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000887 if (!func_is_global(ufunc))
888 {
889 res = generate_CALL(cctx, ufunc, argcount);
890 goto theend;
891 }
892 if (!has_g_namespace
893 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
894 {
895 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100896 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000897 goto theend;
898 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000899 }
900 }
901
902 // If the name is a variable, load it and use PCALL.
903 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000904 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000905 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000906 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000907 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
908 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000909 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000910
911 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
912 goto theend;
913 }
914
915 // If we can find a global function by name generate the right call.
916 if (ufunc != NULL)
917 {
918 res = generate_CALL(cctx, ufunc, argcount);
919 goto theend;
920 }
921
922 // A global function may be defined only later. Need to figure out at
923 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000924 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000925 res = generate_UCALL(cctx, name, argcount);
926 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100927 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000928
929theend:
930 vim_free(tofree);
931 return res;
932}
933
934// like NAMESPACE_CHAR but with 'a' and 'l'.
935#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
936
937/*
938 * Find the end of a variable or function name. Unlike find_name_end() this
939 * does not recognize magic braces.
940 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
941 * Return a pointer to just after the name. Equal to "arg" if there is no
942 * valid name.
943 */
944 char_u *
945to_name_end(char_u *arg, int use_namespace)
946{
947 char_u *p;
948
949 // Quick check for valid starting character.
950 if (!eval_isnamec1(*arg))
951 return arg;
952
953 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
954 // Include a namespace such as "s:var" and "v:var". But "n:" is not
955 // and can be used in slice "[n:]".
956 if (*p == ':' && (p != arg + 1
957 || !use_namespace
958 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
959 break;
960 return p;
961}
962
963/*
964 * Like to_name_end() but also skip over a list or dict constant.
965 * Also accept "<SNR>123_Func".
966 * This intentionally does not handle line continuation.
967 */
968 char_u *
969to_name_const_end(char_u *arg)
970{
971 char_u *p = arg;
972 typval_T rettv;
973
974 if (STRNCMP(p, "<SNR>", 5) == 0)
975 p = skipdigits(p + 5);
976 p = to_name_end(p, TRUE);
977 if (p == arg && *arg == '[')
978 {
979
980 // Can be "[1, 2, 3]->Func()".
981 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
982 p = arg;
983 }
984 return p;
985}
986
987/*
988 * parse a list: [expr, expr]
989 * "*arg" points to the '['.
990 * ppconst->pp_is_const is set if all items are a constant.
991 */
992 static int
993compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
994{
995 char_u *p = skipwhite(*arg + 1);
996 char_u *whitep = *arg + 1;
997 int count = 0;
998 int is_const;
999 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001000 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001001
1002 for (;;)
1003 {
1004 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1005 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001006 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001007 return FAIL;
1008 }
1009 if (*p == ',')
1010 {
1011 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1012 return FAIL;
1013 }
1014 if (*p == ']')
1015 {
1016 ++p;
1017 break;
1018 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001019 if (must_end)
1020 {
1021 semsg(_(e_missing_comma_in_list_str), p);
1022 return FAIL;
1023 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001024 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1025 return FAIL;
1026 if (!is_const)
1027 is_all_const = FALSE;
1028 ++count;
1029 if (*p == ',')
1030 {
1031 ++p;
1032 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1033 {
1034 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1035 return FAIL;
1036 }
1037 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001038 else
1039 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001040 whitep = p;
1041 p = skipwhite(p);
1042 }
1043 *arg = p;
1044
1045 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001046 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001047}
1048
1049/*
1050 * Parse a lambda: "(arg, arg) => expr"
1051 * "*arg" points to the '('.
1052 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1053 */
1054 static int
1055compile_lambda(char_u **arg, cctx_T *cctx)
1056{
1057 int r;
1058 typval_T rettv;
1059 ufunc_T *ufunc;
1060 evalarg_T evalarg;
1061
1062 init_evalarg(&evalarg);
1063 evalarg.eval_flags = EVAL_EVALUATE;
1064 evalarg.eval_cctx = cctx;
1065
1066 // Get the funcref in "rettv".
1067 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1068 if (r != OK)
1069 {
1070 clear_evalarg(&evalarg, NULL);
1071 return r;
1072 }
1073
1074 // "rettv" will now be a partial referencing the function.
1075 ufunc = rettv.vval.v_partial->pt_func;
1076 ++ufunc->uf_refcount;
1077 clear_tv(&rettv);
1078
1079 // Compile it here to get the return type. The return type is optional,
1080 // when it's missing use t_unknown. This is recognized in
1081 // compile_return().
1082 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1083 ufunc->uf_ret_type = &t_unknown;
1084 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1085
1086 // When the outer function is compiled for profiling or debugging, the
1087 // lambda may be called without profiling or debugging. Compile it here in
1088 // the right context.
1089 if (cctx->ctx_compile_type == CT_DEBUG
1090#ifdef FEAT_PROFILE
1091 || cctx->ctx_compile_type == CT_PROFILE
1092#endif
1093 )
1094 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1095
Bram Moolenaar139575d2022-03-15 19:29:30 +00001096 // if the outer function is not compiled for debugging or profiling, this
1097 // one might be
1098 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001099 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001100 compiletype_T compile_type = get_compile_type(ufunc);
1101
1102 if (compile_type != CT_NONE)
1103 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001104 }
1105
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001106 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1107 // "*arg" may point into it. Point into the original line to avoid a
1108 // dangling pointer.
1109 if (evalarg.eval_using_cmdline)
1110 {
1111 garray_T *gap = &evalarg.eval_tofree_ga;
1112 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1113
1114 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1115 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001116 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001117 }
1118
1119 clear_evalarg(&evalarg, NULL);
1120
1121 if (ufunc->uf_def_status == UF_COMPILED)
1122 {
1123 // The return type will now be known.
1124 set_function_type(ufunc);
1125
1126 // The function reference count will be 1. When the ISN_FUNCREF
1127 // instruction is deleted the reference count is decremented and the
1128 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001129 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001130 }
1131
1132 func_ptr_unref(ufunc);
1133 return FAIL;
1134}
1135
1136/*
1137 * Get a lambda and compile it. Uses Vim9 syntax.
1138 */
1139 int
1140get_lambda_tv_and_compile(
1141 char_u **arg,
1142 typval_T *rettv,
1143 int types_optional,
1144 evalarg_T *evalarg)
1145{
1146 int r;
1147 ufunc_T *ufunc;
1148 int save_sc_version = current_sctx.sc_version;
1149
1150 // Get the funcref in "rettv".
1151 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1152 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1153 current_sctx.sc_version = save_sc_version;
1154 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001155 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001156
1157 // "rettv" will now be a partial referencing the function.
1158 ufunc = rettv->vval.v_partial->pt_func;
1159
1160 // Compile it here to get the return type. The return type is optional,
1161 // when it's missing use t_unknown. This is recognized in
1162 // compile_return().
1163 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1164 ufunc->uf_ret_type = &t_unknown;
1165 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1166
1167 if (ufunc->uf_def_status == UF_COMPILED)
1168 {
1169 // The return type will now be known.
1170 set_function_type(ufunc);
1171 return OK;
1172 }
1173 clear_tv(rettv);
1174 return FAIL;
1175}
1176
1177/*
1178 * parse a dict: {key: val, [key]: val}
1179 * "*arg" points to the '{'.
1180 * ppconst->pp_is_const is set if all item values are a constant.
1181 */
1182 static int
1183compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1184{
1185 garray_T *instr = &cctx->ctx_instr;
1186 int count = 0;
1187 dict_T *d = dict_alloc();
1188 dictitem_T *item;
1189 char_u *whitep = *arg + 1;
1190 char_u *p;
1191 int is_const;
1192 int is_all_const = TRUE; // reset when non-const encountered
1193
1194 if (d == NULL)
1195 return FAIL;
1196 if (generate_ppconst(cctx, ppconst) == FAIL)
1197 return FAIL;
1198 for (;;)
1199 {
1200 char_u *key = NULL;
1201
1202 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1203 {
1204 *arg = NULL;
1205 goto failret;
1206 }
1207
1208 if (**arg == '}')
1209 break;
1210
1211 if (**arg == '[')
1212 {
1213 isn_T *isn;
1214
1215 // {[expr]: value} uses an evaluated key.
1216 *arg = skipwhite(*arg + 1);
1217 if (compile_expr0(arg, cctx) == FAIL)
1218 return FAIL;
1219 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1220 if (isn->isn_type == ISN_PUSHNR)
1221 {
1222 char buf[NUMBUFLEN];
1223
1224 // Convert to string at compile time.
1225 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1226 isn->isn_type = ISN_PUSHS;
1227 isn->isn_arg.string = vim_strsave((char_u *)buf);
1228 }
1229 if (isn->isn_type == ISN_PUSHS)
1230 key = isn->isn_arg.string;
1231 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1232 return FAIL;
1233 *arg = skipwhite(*arg);
1234 if (**arg != ']')
1235 {
1236 emsg(_(e_missing_matching_bracket_after_dict_key));
1237 return FAIL;
1238 }
1239 ++*arg;
1240 }
1241 else
1242 {
1243 // {"name": value},
1244 // {'name': value},
1245 // {name: value} use "name" as a literal key
1246 key = get_literal_key(arg);
1247 if (key == NULL)
1248 return FAIL;
1249 if (generate_PUSHS(cctx, &key) == FAIL)
1250 return FAIL;
1251 }
1252
1253 // Check for duplicate keys, if using string keys.
1254 if (key != NULL)
1255 {
1256 item = dict_find(d, key, -1);
1257 if (item != NULL)
1258 {
dundargocc57b5bc2022-11-02 13:30:51 +00001259 semsg(_(e_duplicate_key_in_dictionary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001260 goto failret;
1261 }
1262 item = dictitem_alloc(key);
1263 if (item != NULL)
1264 {
1265 item->di_tv.v_type = VAR_UNKNOWN;
1266 item->di_tv.v_lock = 0;
1267 if (dict_add(d, item) == FAIL)
1268 dictitem_free(item);
1269 }
1270 }
1271
1272 if (**arg != ':')
1273 {
1274 if (*skipwhite(*arg) == ':')
1275 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1276 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001277 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001278 return FAIL;
1279 }
1280 whitep = *arg + 1;
1281 if (!IS_WHITE_OR_NUL(*whitep))
1282 {
1283 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1284 return FAIL;
1285 }
1286
1287 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1288 {
1289 *arg = NULL;
1290 goto failret;
1291 }
1292
1293 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1294 return FAIL;
1295 if (!is_const)
1296 is_all_const = FALSE;
1297 ++count;
1298
1299 whitep = *arg;
1300 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1301 {
1302 *arg = NULL;
1303 goto failret;
1304 }
1305 if (**arg == '}')
1306 break;
1307 if (**arg != ',')
1308 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001309 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001310 goto failret;
1311 }
1312 if (IS_WHITE_OR_NUL(*whitep))
1313 {
1314 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1315 return FAIL;
1316 }
1317 whitep = *arg + 1;
1318 if (!IS_WHITE_OR_NUL(*whitep))
1319 {
1320 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1321 return FAIL;
1322 }
1323 *arg = skipwhite(whitep);
1324 }
1325
1326 *arg = *arg + 1;
1327
1328 // Allow for following comment, after at least one space.
1329 p = skipwhite(*arg);
1330 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1331 *arg += STRLEN(*arg);
1332
1333 dict_unref(d);
1334 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001335 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001336
1337failret:
1338 if (*arg == NULL)
1339 {
1340 semsg(_(e_missing_dict_end), _("[end of lines]"));
1341 *arg = (char_u *)"";
1342 }
1343 dict_unref(d);
1344 return FAIL;
1345}
1346
1347/*
1348 * Compile "&option".
1349 */
1350 static int
1351compile_get_option(char_u **arg, cctx_T *cctx)
1352{
1353 typval_T rettv;
1354 char_u *start = *arg;
1355 int ret;
1356
1357 // parse the option and get the current value to get the type.
1358 rettv.v_type = VAR_UNKNOWN;
1359 ret = eval_option(arg, &rettv, TRUE);
1360 if (ret == OK)
1361 {
1362 // include the '&' in the name, eval_option() expects it.
1363 char_u *name = vim_strnsave(start, *arg - start);
1364 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1365 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1366
1367 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1368 vim_free(name);
1369 }
1370 clear_tv(&rettv);
1371
1372 return ret;
1373}
1374
1375/*
1376 * Compile "$VAR".
1377 */
1378 static int
1379compile_get_env(char_u **arg, cctx_T *cctx)
1380{
1381 char_u *start = *arg;
1382 int len;
1383 int ret;
1384 char_u *name;
1385
1386 ++*arg;
1387 len = get_env_len(arg);
1388 if (len == 0)
1389 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001390 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001391 return FAIL;
1392 }
1393
1394 // include the '$' in the name, eval_env_var() expects it.
1395 name = vim_strnsave(start, len + 1);
1396 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1397 vim_free(name);
1398 return ret;
1399}
1400
1401/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001402 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001403 */
1404 static int
1405compile_interp_string(char_u **arg, cctx_T *cctx)
1406{
1407 typval_T tv;
1408 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001409 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001410 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001411 int count = 0;
1412 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001413
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001414 // *arg is on the '$' character, move it to the first string character.
1415 ++*arg;
1416 quote = **arg;
1417 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001418
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001419 for (;;)
1420 {
1421 // Get the string up to the matching quote or to a single '{'.
1422 // "arg" is advanced to either the quote or the '{'.
1423 if (quote == '"')
1424 ret = eval_string(arg, &tv, evaluate, TRUE);
1425 else
1426 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1427 if (ret == FAIL)
1428 break;
1429 if (evaluate)
1430 {
1431 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1432 || (**arg != '{' && count == 0))
1433 {
1434 // generate non-empty string or empty string if it's the only
1435 // one
1436 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1437 return FAIL;
1438 tv.vval.v_string = NULL; // don't free it now
1439 ++count;
1440 }
1441 clear_tv(&tv);
1442 }
1443
1444 if (**arg != '{')
1445 {
1446 // found terminating quote
1447 ++*arg;
1448 break;
1449 }
1450
1451 p = compile_one_expr_in_str(*arg, cctx);
1452 if (p == NULL)
1453 {
1454 ret = FAIL;
1455 break;
1456 }
1457 ++count;
1458 *arg = p;
1459 }
LemonBoy2eaef102022-05-06 13:14:50 +01001460
1461 if (ret == FAIL || !evaluate)
1462 return ret;
1463
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001464 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1465 if (count > 1)
1466 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001467
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001468 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001469}
1470
1471/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001472 * Compile "@r".
1473 */
1474 static int
1475compile_get_register(char_u **arg, cctx_T *cctx)
1476{
1477 int ret;
1478
1479 ++*arg;
1480 if (**arg == NUL)
1481 {
1482 semsg(_(e_syntax_error_at_str), *arg - 1);
1483 return FAIL;
1484 }
1485 if (!valid_yank_reg(**arg, FALSE))
1486 {
1487 emsg_invreg(**arg);
1488 return FAIL;
1489 }
1490 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1491 ++*arg;
1492 return ret;
1493}
1494
1495/*
1496 * Apply leading '!', '-' and '+' to constant "rettv".
1497 * When "numeric_only" is TRUE do not apply '!'.
1498 */
1499 static int
1500apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1501{
1502 char_u *p = *end;
1503
1504 // this works from end to start
1505 while (p > start)
1506 {
1507 --p;
1508 if (*p == '-' || *p == '+')
1509 {
1510 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001511 if (rettv->v_type == VAR_FLOAT)
1512 {
1513 if (*p == '-')
1514 rettv->vval.v_float = -rettv->vval.v_float;
1515 }
1516 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001517 {
1518 varnumber_T val;
1519 int error = FALSE;
1520
1521 // tv_get_number_chk() accepts a string, but we don't want that
1522 // here
1523 if (check_not_string(rettv) == FAIL)
1524 return FAIL;
1525 val = tv_get_number_chk(rettv, &error);
1526 clear_tv(rettv);
1527 if (error)
1528 return FAIL;
1529 if (*p == '-')
1530 val = -val;
1531 rettv->v_type = VAR_NUMBER;
1532 rettv->vval.v_number = val;
1533 }
1534 }
1535 else if (numeric_only)
1536 {
1537 ++p;
1538 break;
1539 }
1540 else if (*p == '!')
1541 {
1542 int v = tv2bool(rettv);
1543
1544 // '!' is permissive in the type.
1545 clear_tv(rettv);
1546 rettv->v_type = VAR_BOOL;
1547 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1548 }
1549 }
1550 *end = p;
1551 return OK;
1552}
1553
1554/*
1555 * Recognize v: variables that are constants and set "rettv".
1556 */
1557 static void
1558get_vim_constant(char_u **arg, typval_T *rettv)
1559{
1560 if (STRNCMP(*arg, "v:true", 6) == 0)
1561 {
1562 rettv->v_type = VAR_BOOL;
1563 rettv->vval.v_number = VVAL_TRUE;
1564 *arg += 6;
1565 }
1566 else if (STRNCMP(*arg, "v:false", 7) == 0)
1567 {
1568 rettv->v_type = VAR_BOOL;
1569 rettv->vval.v_number = VVAL_FALSE;
1570 *arg += 7;
1571 }
1572 else if (STRNCMP(*arg, "v:null", 6) == 0)
1573 {
1574 rettv->v_type = VAR_SPECIAL;
1575 rettv->vval.v_number = VVAL_NULL;
1576 *arg += 6;
1577 }
1578 else if (STRNCMP(*arg, "v:none", 6) == 0)
1579 {
1580 rettv->v_type = VAR_SPECIAL;
1581 rettv->vval.v_number = VVAL_NONE;
1582 *arg += 6;
1583 }
1584}
1585
1586 exprtype_T
1587get_compare_type(char_u *p, int *len, int *type_is)
1588{
1589 exprtype_T type = EXPR_UNKNOWN;
1590 int i;
1591
1592 switch (p[0])
1593 {
1594 case '=': if (p[1] == '=')
1595 type = EXPR_EQUAL;
1596 else if (p[1] == '~')
1597 type = EXPR_MATCH;
1598 break;
1599 case '!': if (p[1] == '=')
1600 type = EXPR_NEQUAL;
1601 else if (p[1] == '~')
1602 type = EXPR_NOMATCH;
1603 break;
1604 case '>': if (p[1] != '=')
1605 {
1606 type = EXPR_GREATER;
1607 *len = 1;
1608 }
1609 else
1610 type = EXPR_GEQUAL;
1611 break;
1612 case '<': if (p[1] != '=')
1613 {
1614 type = EXPR_SMALLER;
1615 *len = 1;
1616 }
1617 else
1618 type = EXPR_SEQUAL;
1619 break;
1620 case 'i': if (p[1] == 's')
1621 {
1622 // "is" and "isnot"; but not a prefix of a name
1623 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1624 *len = 5;
1625 i = p[*len];
1626 if (!isalnum(i) && i != '_')
1627 {
1628 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1629 *type_is = TRUE;
1630 }
1631 }
1632 break;
1633 }
1634 return type;
1635}
1636
1637/*
1638 * Skip over an expression, ignoring most errors.
1639 */
1640 void
1641skip_expr_cctx(char_u **arg, cctx_T *cctx)
1642{
1643 evalarg_T evalarg;
1644
1645 init_evalarg(&evalarg);
1646 evalarg.eval_cctx = cctx;
1647 skip_expr(arg, &evalarg);
1648 clear_evalarg(&evalarg, NULL);
1649}
1650
1651/*
1652 * Check that the top of the type stack has a type that can be used as a
1653 * condition. Give an error and return FAIL if not.
1654 */
1655 int
1656bool_on_stack(cctx_T *cctx)
1657{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001658 type_T *type;
1659
Bram Moolenaar078a4612022-01-04 15:17:03 +00001660 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001661 if (type == &t_bool)
1662 return OK;
1663
Bram Moolenaar4913d422022-10-17 13:13:32 +01001664 if (type->tt_type == VAR_ANY
1665 || type->tt_type == VAR_UNKNOWN
1666 || type->tt_type == VAR_NUMBER
1667 || type == &t_number_bool
1668 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001669 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1670 // This requires a runtime type check.
1671 return generate_COND2BOOL(cctx);
1672
1673 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1674}
1675
1676/*
1677 * Give the "white on both sides" error, taking the operator from "p[len]".
1678 */
1679 void
1680error_white_both(char_u *op, int len)
1681{
1682 char_u buf[10];
1683
1684 vim_strncpy(buf, op, len);
1685 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1686}
1687
1688/*
1689 * Compile code to apply '-', '+' and '!'.
1690 * When "numeric_only" is TRUE do not apply '!'.
1691 */
1692 static int
1693compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1694{
1695 char_u *p = *end;
1696
1697 // this works from end to start
1698 while (p > start)
1699 {
1700 --p;
1701 while (VIM_ISWHITE(*p))
1702 --p;
1703 if (*p == '-' || *p == '+')
1704 {
1705 int negate = *p == '-';
1706 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001707 type_T *type;
1708
Bram Moolenaar078a4612022-01-04 15:17:03 +00001709 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001710 if (type != &t_float && need_type(type, &t_number,
1711 -1, 0, cctx, FALSE, FALSE) == FAIL)
1712 return FAIL;
1713
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001714 // only '-' has an effect, for '+' we only check the type
1715 if (negate)
1716 {
1717 isn = generate_instr(cctx, ISN_NEGATENR);
1718 if (isn == NULL)
1719 return FAIL;
1720 }
1721 }
1722 else if (numeric_only)
1723 {
1724 ++p;
1725 break;
1726 }
1727 else
1728 {
1729 int invert = *p == '!';
1730
1731 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1732 {
1733 if (p[-1] == '!')
1734 invert = !invert;
1735 --p;
1736 }
1737 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1738 return FAIL;
1739 }
1740 }
1741 *end = p;
1742 return OK;
1743}
1744
1745/*
1746 * Compile "(expression)": recursive!
1747 * Return FAIL/OK.
1748 */
1749 static int
1750compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1751{
1752 int ret;
1753 char_u *p = *arg + 1;
1754
1755 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1756 return FAIL;
1757 if (ppconst->pp_used <= PPSIZE - 10)
1758 {
1759 ret = compile_expr1(arg, cctx, ppconst);
1760 }
1761 else
1762 {
1763 // Not enough space in ppconst, flush constants.
1764 if (generate_ppconst(cctx, ppconst) == FAIL)
1765 return FAIL;
1766 ret = compile_expr0(arg, cctx);
1767 }
1768 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1769 return FAIL;
1770 if (**arg == ')')
1771 ++*arg;
1772 else if (ret == OK)
1773 {
1774 emsg(_(e_missing_closing_paren));
1775 ret = FAIL;
1776 }
1777 return ret;
1778}
1779
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001780static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001781
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001782/*
1783 * Compile whatever comes after "name" or "name()".
1784 * Advances "*arg" only when something was recognized.
1785 */
1786 static int
1787compile_subscript(
1788 char_u **arg,
1789 cctx_T *cctx,
1790 char_u *start_leader,
1791 char_u **end_leader,
1792 ppconst_T *ppconst)
1793{
1794 char_u *name_start = *end_leader;
1795 int keeping_dict = FALSE;
1796
1797 for (;;)
1798 {
1799 char_u *p = skipwhite(*arg);
1800
1801 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1802 {
1803 char_u *next = peek_next_line_from_context(cctx);
1804
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001805 // If a following line starts with "->{", "->(" or "->X" advance to
1806 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001807 // Also if a following line starts with ".x".
1808 if (next != NULL &&
1809 ((next[0] == '-' && next[1] == '>'
1810 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001811 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001812 || ASCII_ISALPHA(*skipwhite(next + 2))))
1813 || (next[0] == '.' && eval_isdictc(next[1]))))
1814 {
1815 next = next_line_from_context(cctx, TRUE);
1816 if (next == NULL)
1817 return FAIL;
1818 *arg = next;
1819 p = skipwhite(*arg);
1820 }
1821 }
1822
1823 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1824 // is not a function call.
1825 if (**arg == '(')
1826 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001827 type_T *type;
1828 int argcount = 0;
1829
1830 if (generate_ppconst(cctx, ppconst) == FAIL)
1831 return FAIL;
1832 ppconst->pp_is_const = FALSE;
1833
1834 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001835 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001836
1837 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001838 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001839 return FAIL;
1840 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1841 return FAIL;
1842 if (keeping_dict)
1843 {
1844 keeping_dict = FALSE;
1845 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1846 return FAIL;
1847 }
1848 }
1849 else if (*p == '-' && p[1] == '>')
1850 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001851 char_u *pstart = p;
1852 int alt;
1853 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001854
Bram Moolenaarc7349932022-01-16 20:59:39 +00001855 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001856 if (generate_ppconst(cctx, ppconst) == FAIL)
1857 return FAIL;
1858 ppconst->pp_is_const = FALSE;
1859
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001860 // Apply the '!', '-' and '+' first:
1861 // -1.0->func() works like (-1.0)->func()
1862 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1863 return FAIL;
1864
1865 p += 2;
1866 *arg = skipwhite(p);
1867 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001868
1869 // Three alternatives handled here:
1870 // 1. "base->name(" only a name, use compile_call()
1871 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1872 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001873 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001874 alt = 2;
1875 else
1876 {
1877 // alternative 1 or 3
1878 p = *arg;
1879 if (!eval_isnamec1(*p))
1880 {
1881 semsg(_(e_trailing_characters_str), pstart);
1882 return FAIL;
1883 }
1884 if (ASCII_ISALPHA(*p) && p[1] == ':')
1885 p += 2;
1886 for ( ; eval_isnamec(*p); ++p)
1887 ;
1888 if (*p == '(')
1889 {
1890 // alternative 1
1891 alt = 1;
1892 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1893 return FAIL;
1894 }
1895 else
1896 {
1897 // Must be alternative 3, find the "(". Only works within
1898 // one line.
1899 alt = 3;
1900 paren = vim_strchr(p, '(');
1901 if (paren == NULL)
1902 {
1903 semsg(_(e_missing_parenthesis_str), *arg);
1904 return FAIL;
1905 }
1906 }
1907 }
1908
1909 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001910 {
1911 int argcount = 1;
1912 garray_T *stack = &cctx->ctx_type_stack;
1913 int type_idx_start = stack->ga_len;
1914 type_T *type;
1915 int expr_isn_start = cctx->ctx_instr.ga_len;
1916 int expr_isn_end;
1917 int arg_isn_count;
1918
Bram Moolenaarc7349932022-01-16 20:59:39 +00001919 if (alt == 2)
1920 {
1921 // Funcref call: list->(Refs[2])(arg)
1922 // or lambda: list->((arg) => expr)(arg)
1923 //
1924 // Fist compile the function expression.
1925 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1926 return FAIL;
1927 }
1928 else
1929 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001930 int fail;
1931 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001932 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001933
Bram Moolenaarc7349932022-01-16 20:59:39 +00001934 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001935
1936 // instead of using LOADG for "import.Func" use PUSHFUNC
1937 ++paren_follows_after_expr;
1938
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001939 // do not look in the next line
1940 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001941
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001942 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001943 || *skipwhite(*arg) != NUL;
1944 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001945 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001946 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001947
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001948 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001949 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001950 if (did_emsg == prev_did_emsg)
1951 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001952 return FAIL;
1953 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001954 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001955
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001956 // Compile the arguments.
1957 if (**arg != '(')
1958 {
1959 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001960 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001961 else
1962 semsg(_(e_missing_parenthesis_str), *arg);
1963 return FAIL;
1964 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001965
1966 // Remember the next instruction index, where the instructions
1967 // for arguments are being written.
1968 expr_isn_end = cctx->ctx_instr.ga_len;
1969
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001970 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001971 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
1972 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001973 return FAIL;
1974
1975 // Move the instructions for the arguments to before the
1976 // instructions of the expression and move the type of the
1977 // expression after the argument types. This is what ISN_PCALL
1978 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001979 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1980 if (arg_isn_count > 0)
1981 {
1982 int expr_isn_count = expr_isn_end - expr_isn_start;
1983 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001984 type_T *decl_type;
1985 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001986
1987 if (isn == NULL)
1988 return FAIL;
1989 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1990 + expr_isn_start,
1991 sizeof(isn_T) * expr_isn_count);
1992 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1993 + expr_isn_start,
1994 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1995 sizeof(isn_T) * arg_isn_count);
1996 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1997 + expr_isn_start + arg_isn_count,
1998 isn, sizeof(isn_T) * expr_isn_count);
1999 vim_free(isn);
2000
Bram Moolenaar078a4612022-01-04 15:17:03 +00002001 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2002 type = typep->type_curr;
2003 decl_type = typep->type_decl;
2004 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2005 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2006 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002007 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002008 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2009 typep->type_curr = type;
2010 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002011 }
2012
Bram Moolenaar078a4612022-01-04 15:17:03 +00002013 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002014 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2015 return FAIL;
2016 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002017
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002018 if (keeping_dict)
2019 {
2020 keeping_dict = FALSE;
2021 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2022 return FAIL;
2023 }
2024 }
2025 else if (**arg == '[')
2026 {
2027 int is_slice = FALSE;
2028
2029 // list index: list[123]
2030 // dict member: dict[key]
2031 // string index: text[123]
2032 // blob index: blob[123]
2033 if (generate_ppconst(cctx, ppconst) == FAIL)
2034 return FAIL;
2035 ppconst->pp_is_const = FALSE;
2036
2037 ++p;
2038 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2039 return FAIL;
2040 if (**arg == ':')
2041 {
2042 // missing first index is equal to zero
2043 generate_PUSHNR(cctx, 0);
2044 }
2045 else
2046 {
2047 if (compile_expr0(arg, cctx) == FAIL)
2048 return FAIL;
2049 if (**arg == ':')
2050 {
2051 semsg(_(e_white_space_required_before_and_after_str_at_str),
2052 ":", *arg);
2053 return FAIL;
2054 }
2055 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2056 return FAIL;
2057 *arg = skipwhite(*arg);
2058 }
2059 if (**arg == ':')
2060 {
2061 is_slice = TRUE;
2062 ++*arg;
2063 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2064 {
2065 semsg(_(e_white_space_required_before_and_after_str_at_str),
2066 ":", *arg);
2067 return FAIL;
2068 }
2069 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2070 return FAIL;
2071 if (**arg == ']')
2072 // missing second index is equal to end of string
2073 generate_PUSHNR(cctx, -1);
2074 else
2075 {
2076 if (compile_expr0(arg, cctx) == FAIL)
2077 return FAIL;
2078 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2079 return FAIL;
2080 *arg = skipwhite(*arg);
2081 }
2082 }
2083
2084 if (**arg != ']')
2085 {
2086 emsg(_(e_missing_closing_square_brace));
2087 return FAIL;
2088 }
2089 *arg = *arg + 1;
2090
2091 if (keeping_dict)
2092 {
2093 keeping_dict = FALSE;
2094 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2095 return FAIL;
2096 }
2097 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2098 return FAIL;
2099 }
2100 else if (*p == '.' && p[1] != '.')
2101 {
2102 // dictionary member: dict.name
2103 if (generate_ppconst(cctx, ppconst) == FAIL)
2104 return FAIL;
2105 ppconst->pp_is_const = FALSE;
2106
2107 *arg = p + 1;
2108 if (IS_WHITE_OR_NUL(**arg))
2109 {
2110 emsg(_(e_missing_name_after_dot));
2111 return FAIL;
2112 }
2113 p = *arg;
2114 if (eval_isdictc(*p))
2115 while (eval_isnamec(*p))
2116 MB_PTR_ADV(p);
2117 if (p == *arg)
2118 {
2119 semsg(_(e_syntax_error_at_str), *arg);
2120 return FAIL;
2121 }
2122 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2123 return FAIL;
2124 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2125 return FAIL;
2126 keeping_dict = TRUE;
2127 *arg = p;
2128 }
2129 else
2130 break;
2131 }
2132
2133 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2134 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002135 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2136 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002137 return FAIL;
2138
2139 return OK;
2140}
2141
2142/*
2143 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2144 * "arg" is advanced until after the expression, skipping white space.
2145 *
2146 * If the value is a constant "ppconst->pp_used" will be non-zero.
2147 * Before instructions are generated, any values in "ppconst" will generated.
2148 *
2149 * This is the compiling equivalent of eval1(), eval2(), etc.
2150 */
2151
2152/*
2153 * number number constant
2154 * 0zFFFFFFFF Blob constant
2155 * "string" string constant
2156 * 'string' literal string constant
2157 * &option-name option value
2158 * @r register contents
2159 * identifier variable value
2160 * function() function call
2161 * $VAR environment variable
2162 * (expression) nested expression
2163 * [expr, expr] List
2164 * {key: val, [key]: val} Dictionary
2165 *
2166 * Also handle:
2167 * ! in front logical NOT
2168 * - in front unary minus
2169 * + in front unary plus (ignored)
2170 * trailing (arg) funcref/partial call
2171 * trailing [] subscript in String or List
2172 * trailing .name entry in Dictionary
2173 * trailing ->name() method call
2174 */
2175 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002176compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002177 char_u **arg,
2178 cctx_T *cctx,
2179 ppconst_T *ppconst)
2180{
2181 char_u *start_leader, *end_leader;
2182 int ret = OK;
2183 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2184 int used_before = ppconst->pp_used;
2185
2186 ppconst->pp_is_const = FALSE;
2187
2188 /*
2189 * Skip '!', '-' and '+' characters. They are handled later.
2190 */
2191 start_leader = *arg;
2192 if (eval_leader(arg, TRUE) == FAIL)
2193 return FAIL;
2194 end_leader = *arg;
2195
2196 rettv->v_type = VAR_UNKNOWN;
2197 switch (**arg)
2198 {
2199 /*
2200 * Number constant.
2201 */
2202 case '0': // also for blob starting with 0z
2203 case '1':
2204 case '2':
2205 case '3':
2206 case '4':
2207 case '5':
2208 case '6':
2209 case '7':
2210 case '8':
2211 case '9':
2212 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2213 return FAIL;
2214 // Apply "-" and "+" just before the number now, right to
2215 // left. Matters especially when "->" follows. Stops at
2216 // '!'.
2217 if (apply_leader(rettv, TRUE,
2218 start_leader, &end_leader) == FAIL)
2219 {
2220 clear_tv(rettv);
2221 return FAIL;
2222 }
2223 break;
2224
2225 /*
2226 * String constant: "string".
2227 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002228 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002229 return FAIL;
2230 break;
2231
2232 /*
2233 * Literal string constant: 'str''ing'.
2234 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002235 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002236 return FAIL;
2237 break;
2238
2239 /*
2240 * Constant Vim variable.
2241 */
2242 case 'v': get_vim_constant(arg, rettv);
2243 ret = NOTDONE;
2244 break;
2245
2246 /*
2247 * "true" constant
2248 */
2249 case 't': if (STRNCMP(*arg, "true", 4) == 0
2250 && !eval_isnamec((*arg)[4]))
2251 {
2252 *arg += 4;
2253 rettv->v_type = VAR_BOOL;
2254 rettv->vval.v_number = VVAL_TRUE;
2255 }
2256 else
2257 ret = NOTDONE;
2258 break;
2259
2260 /*
2261 * "false" constant
2262 */
2263 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2264 && !eval_isnamec((*arg)[5]))
2265 {
2266 *arg += 5;
2267 rettv->v_type = VAR_BOOL;
2268 rettv->vval.v_number = VVAL_FALSE;
2269 }
2270 else
2271 ret = NOTDONE;
2272 break;
2273
2274 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002275 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002276 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002277 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002278 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002279 char_u *p = *arg + 4;
2280 int len;
2281
2282 for (len = 0; eval_isnamec(p[len]); ++len)
2283 ;
2284 ret = handle_predefined(*arg, len + 4, rettv);
2285 if (ret == FAIL)
2286 ret = NOTDONE;
2287 else
2288 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002289 }
2290 else
2291 ret = NOTDONE;
2292 break;
2293
2294 /*
2295 * List: [expr, expr]
2296 */
2297 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2298 return FAIL;
2299 ret = compile_list(arg, cctx, ppconst);
2300 break;
2301
2302 /*
2303 * Dictionary: {'key': val, 'key': val}
2304 */
2305 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2306 return FAIL;
2307 ret = compile_dict(arg, cctx, ppconst);
2308 break;
2309
2310 /*
2311 * Option value: &name
2312 */
2313 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2314 return FAIL;
2315 ret = compile_get_option(arg, cctx);
2316 break;
2317
2318 /*
2319 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002320 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002321 */
2322 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2323 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002324 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2325 ret = compile_interp_string(arg, cctx);
2326 else
2327 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002328 break;
2329
2330 /*
2331 * Register contents: @r.
2332 */
2333 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2334 return FAIL;
2335 ret = compile_get_register(arg, cctx);
2336 break;
2337 /*
2338 * nested expression: (expression).
2339 * lambda: (arg, arg) => expr
2340 * funcref: (arg, arg) => { statement }
2341 */
2342 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2343 ret = compile_lambda(arg, cctx);
2344 if (ret == NOTDONE)
2345 ret = compile_parenthesis(arg, cctx, ppconst);
2346 break;
2347
2348 default: ret = NOTDONE;
2349 break;
2350 }
2351 if (ret == FAIL)
2352 return FAIL;
2353
2354 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2355 {
2356 if (cctx->ctx_skip == SKIP_YES)
2357 clear_tv(rettv);
2358 else
2359 // A constant expression can possibly be handled compile time,
2360 // return the value instead of generating code.
2361 ++ppconst->pp_used;
2362 }
2363 else if (ret == NOTDONE)
2364 {
2365 char_u *p;
2366 int r;
2367
2368 if (!eval_isnamec1(**arg))
2369 {
2370 if (!vim9_bad_comment(*arg))
2371 {
2372 if (ends_excmd(*skipwhite(*arg)))
2373 semsg(_(e_empty_expression_str), *arg);
2374 else
2375 semsg(_(e_name_expected_str), *arg);
2376 }
2377 return FAIL;
2378 }
2379
2380 // "name" or "name()"
2381 p = to_name_end(*arg, TRUE);
2382 if (p - *arg == (size_t)1 && **arg == '_')
2383 {
2384 emsg(_(e_cannot_use_underscore_here));
2385 return FAIL;
2386 }
2387
2388 if (*p == '(')
2389 {
2390 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2391 }
2392 else
2393 {
2394 if (cctx->ctx_skip != SKIP_YES
2395 && generate_ppconst(cctx, ppconst) == FAIL)
2396 return FAIL;
2397 r = compile_load(arg, p, cctx, TRUE, TRUE);
2398 }
2399 if (r == FAIL)
2400 return FAIL;
2401 }
2402
2403 // Handle following "[]", ".member", etc.
2404 // Then deal with prefixed '-', '+' and '!', if not done already.
2405 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2406 ppconst) == FAIL)
2407 return FAIL;
2408 if (ppconst->pp_used > 0)
2409 {
2410 // apply the '!', '-' and '+' before the constant
2411 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2412 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2413 return FAIL;
2414 return OK;
2415 }
2416 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2417 return FAIL;
2418 return OK;
2419}
2420
2421/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002422 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002423 */
2424 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002425compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002426{
2427 type_T *want_type = NULL;
2428
2429 // Recognize <type>
2430 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2431 {
2432 ++*arg;
2433 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2434 if (want_type == NULL)
2435 return FAIL;
2436
2437 if (**arg != '>')
2438 {
2439 if (*skipwhite(*arg) == '>')
2440 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2441 else
2442 emsg(_(e_missing_gt));
2443 return FAIL;
2444 }
2445 ++*arg;
2446 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2447 return FAIL;
2448 }
2449
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002450 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002451 return FAIL;
2452
2453 if (want_type != NULL)
2454 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002455 type_T *actual;
2456 where_T where = WHERE_INIT;
2457
2458 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002459 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002460 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002461 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002462 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2463 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002464 return FAIL;
2465 }
2466 }
2467
2468 return OK;
2469}
2470
2471/*
2472 * * number multiplication
2473 * / number division
2474 * % number modulo
2475 */
2476 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002477compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002478{
2479 char_u *op;
2480 char_u *next;
2481 int ppconst_used = ppconst->pp_used;
2482
2483 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002484 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002485 return FAIL;
2486
2487 /*
2488 * Repeat computing, until no "*", "/" or "%" is following.
2489 */
2490 for (;;)
2491 {
2492 op = may_peek_next_line(cctx, *arg, &next);
2493 if (*op != '*' && *op != '/' && *op != '%')
2494 break;
2495 if (next != NULL)
2496 {
2497 *arg = next_line_from_context(cctx, TRUE);
2498 op = skipwhite(*arg);
2499 }
2500
2501 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2502 {
2503 error_white_both(op, 1);
2504 return FAIL;
2505 }
2506 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2507 return FAIL;
2508
2509 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002510 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002511 return FAIL;
2512
2513 if (ppconst->pp_used == ppconst_used + 2
2514 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2515 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2516 {
2517 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2518 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2519 varnumber_T res = 0;
2520 int failed = FALSE;
2521
2522 // both are numbers: compute the result
2523 switch (*op)
2524 {
2525 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2526 break;
2527 case '/': res = num_divide(tv1->vval.v_number,
2528 tv2->vval.v_number, &failed);
2529 break;
2530 case '%': res = num_modulus(tv1->vval.v_number,
2531 tv2->vval.v_number, &failed);
2532 break;
2533 }
2534 if (failed)
2535 return FAIL;
2536 tv1->vval.v_number = res;
2537 --ppconst->pp_used;
2538 }
2539 else
2540 {
2541 generate_ppconst(cctx, ppconst);
2542 generate_two_op(cctx, op);
2543 }
2544 }
2545
2546 return OK;
2547}
2548
2549/*
2550 * + number addition or list/blobl concatenation
2551 * - number subtraction
2552 * .. string concatenation
2553 */
2554 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002555compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002556{
2557 char_u *op;
2558 char_u *next;
2559 int oplen;
2560 int ppconst_used = ppconst->pp_used;
2561
2562 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002563 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002564 return FAIL;
2565
2566 /*
2567 * Repeat computing, until no "+", "-" or ".." is following.
2568 */
2569 for (;;)
2570 {
2571 op = may_peek_next_line(cctx, *arg, &next);
2572 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2573 break;
2574 if (op[0] == op[1] && *op != '.' && next)
2575 // Finding "++" or "--" on the next line is a separate command.
2576 // But ".." is concatenation.
2577 break;
2578 oplen = (*op == '.' ? 2 : 1);
2579 if (next != NULL)
2580 {
2581 *arg = next_line_from_context(cctx, TRUE);
2582 op = skipwhite(*arg);
2583 }
2584
2585 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2586 {
2587 error_white_both(op, oplen);
2588 return FAIL;
2589 }
2590
2591 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2592 return FAIL;
2593
2594 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002595 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002596 return FAIL;
2597
2598 if (ppconst->pp_used == ppconst_used + 2
2599 && (*op == '.'
2600 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2601 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2602 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2603 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2604 {
2605 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2606 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2607
2608 // concat/subtract/add constant numbers
2609 if (*op == '+')
2610 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2611 else if (*op == '-')
2612 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2613 else
2614 {
2615 // concatenate constant strings
2616 char_u *s1 = tv1->vval.v_string;
2617 char_u *s2 = tv2->vval.v_string;
2618 size_t len1 = STRLEN(s1);
2619
2620 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2621 if (tv1->vval.v_string == NULL)
2622 {
2623 clear_ppconst(ppconst);
2624 return FAIL;
2625 }
2626 mch_memmove(tv1->vval.v_string, s1, len1);
2627 STRCPY(tv1->vval.v_string + len1, s2);
2628 vim_free(s1);
2629 vim_free(s2);
2630 }
2631 --ppconst->pp_used;
2632 }
2633 else
2634 {
2635 generate_ppconst(cctx, ppconst);
2636 ppconst->pp_is_const = FALSE;
2637 if (*op == '.')
2638 {
2639 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2640 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2641 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002642 if (generate_CONCAT(cctx, 2) == FAIL)
2643 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002644 }
2645 else
2646 generate_two_op(cctx, op);
2647 }
2648 }
2649
2650 return OK;
2651}
2652
2653/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002654 * expr6a >> expr6b
2655 * expr6a << expr6b
2656 *
2657 * Produces instructions:
2658 * OPNR bitwise left or right shift
2659 */
2660 static int
2661compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2662{
2663 exprtype_T type = EXPR_UNKNOWN;
2664 char_u *p;
2665 char_u *next;
2666 int len = 2;
2667 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002668 isn_T *isn;
2669
2670 // get the first variable
2671 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2672 return FAIL;
2673
2674 /*
2675 * Repeat computing, until no "+", "-" or ".." is following.
2676 */
2677 for (;;)
2678 {
2679 type = EXPR_UNKNOWN;
2680
2681 p = may_peek_next_line(cctx, *arg, &next);
2682 if (p[0] == '<' && p[1] == '<')
2683 type = EXPR_LSHIFT;
2684 else if (p[0] == '>' && p[1] == '>')
2685 type = EXPR_RSHIFT;
2686
2687 if (type == EXPR_UNKNOWN)
2688 return OK;
2689
2690 // Handle a bitwise left or right shift operator
2691 if (ppconst->pp_used == ppconst_used + 1)
2692 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002693 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002694 {
2695 // left operand should be a number
2696 emsg(_(e_bitshift_ops_must_be_number));
2697 return FAIL;
2698 }
2699 }
2700 else
2701 {
2702 type_T *t = get_type_on_stack(cctx, 0);
2703
2704 if (need_type(t, &t_number, 0, 0, cctx, FALSE, FALSE) == FAIL)
2705 {
2706 emsg(_(e_bitshift_ops_must_be_number));
2707 return FAIL;
2708 }
2709 }
2710
2711 if (next != NULL)
2712 {
2713 *arg = next_line_from_context(cctx, TRUE);
2714 p = skipwhite(*arg);
2715 }
2716
2717 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2718 {
2719 error_white_both(p, len);
2720 return FAIL;
2721 }
2722
2723 // get the second variable
2724 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2725 return FAIL;
2726
2727 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2728 return FAIL;
2729
2730 if (ppconst->pp_used == ppconst_used + 2)
2731 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002732 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2733 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2734
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002735 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002736 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2737 {
2738 // right operand should be a positive number
2739 if (tv2->v_type != VAR_NUMBER)
2740 emsg(_(e_bitshift_ops_must_be_number));
2741 else
dundargocc57b5bc2022-11-02 13:30:51 +00002742 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002743 return FAIL;
2744 }
2745
2746 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2747 tv1->vval.v_number = 0;
2748 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002749 tv1->vval.v_number =
2750 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002751 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002752 tv1->vval.v_number =
2753 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002754 clear_tv(tv2);
2755 --ppconst->pp_used;
2756 }
2757 else
2758 {
2759 if (need_type(get_type_on_stack(cctx, 0), &t_number, 0, 0, cctx,
2760 FALSE, FALSE) == FAIL)
2761 {
2762 emsg(_(e_bitshift_ops_must_be_number));
2763 return FAIL;
2764 }
2765
2766 generate_ppconst(cctx, ppconst);
2767
2768 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2769 if (isn == NULL)
2770 return FAIL;
2771
2772 if (isn != NULL)
2773 isn->isn_arg.op.op_type = type;
2774 }
2775 }
2776
2777 return OK;
2778}
2779
2780/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002781 * expr5a == expr5b
2782 * expr5a =~ expr5b
2783 * expr5a != expr5b
2784 * expr5a !~ expr5b
2785 * expr5a > expr5b
2786 * expr5a >= expr5b
2787 * expr5a < expr5b
2788 * expr5a <= expr5b
2789 * expr5a is expr5b
2790 * expr5a isnot expr5b
2791 *
2792 * Produces instructions:
2793 * EVAL expr5a Push result of "expr5a"
2794 * EVAL expr5b Push result of "expr5b"
2795 * COMPARE one of the compare instructions
2796 */
2797 static int
2798compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2799{
2800 exprtype_T type = EXPR_UNKNOWN;
2801 char_u *p;
2802 char_u *next;
2803 int len = 2;
2804 int type_is = FALSE;
2805 int ppconst_used = ppconst->pp_used;
2806
2807 // get the first variable
2808 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2809 return FAIL;
2810
2811 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002812
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002813 type = get_compare_type(p, &len, &type_is);
2814
2815 /*
2816 * If there is a comparative operator, use it.
2817 */
2818 if (type != EXPR_UNKNOWN)
2819 {
2820 int ic = FALSE; // Default: do not ignore case
2821
2822 if (next != NULL)
2823 {
2824 *arg = next_line_from_context(cctx, TRUE);
2825 p = skipwhite(*arg);
2826 }
2827 if (type_is && (p[len] == '?' || p[len] == '#'))
2828 {
2829 semsg(_(e_invalid_expression_str), *arg);
2830 return FAIL;
2831 }
2832 // extra question mark appended: ignore case
2833 if (p[len] == '?')
2834 {
2835 ic = TRUE;
2836 ++len;
2837 }
2838 // extra '#' appended: match case (ignored)
2839 else if (p[len] == '#')
2840 ++len;
2841 // nothing appended: match case
2842
2843 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2844 {
2845 error_white_both(p, len);
2846 return FAIL;
2847 }
2848
2849 // get the second variable
2850 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2851 return FAIL;
2852
2853 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2854 return FAIL;
2855
2856 if (ppconst->pp_used == ppconst_used + 2)
2857 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002858 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002859 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2860 int ret;
2861
2862 // Both sides are a constant, compute the result now.
2863 // First check for a valid combination of types, this is more
2864 // strict than typval_compare().
2865 if (check_compare_types(type, tv1, tv2) == FAIL)
2866 ret = FAIL;
2867 else
2868 {
2869 ret = typval_compare(tv1, tv2, type, ic);
2870 tv1->v_type = VAR_BOOL;
2871 tv1->vval.v_number = tv1->vval.v_number
2872 ? VVAL_TRUE : VVAL_FALSE;
2873 clear_tv(tv2);
2874 --ppconst->pp_used;
2875 }
2876 return ret;
2877 }
2878
2879 generate_ppconst(cctx, ppconst);
2880 return generate_COMPARE(cctx, type, ic);
2881 }
2882
2883 return OK;
2884}
2885
2886static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2887
2888/*
2889 * Compile || or &&.
2890 */
2891 static int
2892compile_and_or(
2893 char_u **arg,
2894 cctx_T *cctx,
2895 char *op,
2896 ppconst_T *ppconst,
2897 int ppconst_used UNUSED)
2898{
2899 char_u *next;
2900 char_u *p = may_peek_next_line(cctx, *arg, &next);
2901 int opchar = *op;
2902
2903 if (p[0] == opchar && p[1] == opchar)
2904 {
2905 garray_T *instr = &cctx->ctx_instr;
2906 garray_T end_ga;
2907 int save_skip = cctx->ctx_skip;
2908
2909 /*
2910 * Repeat until there is no following "||" or "&&"
2911 */
2912 ga_init2(&end_ga, sizeof(int), 10);
2913 while (p[0] == opchar && p[1] == opchar)
2914 {
2915 long start_lnum = SOURCING_LNUM;
2916 long save_sourcing_lnum;
2917 int start_ctx_lnum = cctx->ctx_lnum;
2918 int save_lnum;
2919 int const_used;
2920 int status;
2921 jumpwhen_T jump_when = opchar == '|'
2922 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2923
2924 if (next != NULL)
2925 {
2926 *arg = next_line_from_context(cctx, TRUE);
2927 p = skipwhite(*arg);
2928 }
2929
2930 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2931 {
2932 semsg(_(e_white_space_required_before_and_after_str_at_str),
2933 op, p);
2934 ga_clear(&end_ga);
2935 return FAIL;
2936 }
2937
2938 save_sourcing_lnum = SOURCING_LNUM;
2939 SOURCING_LNUM = start_lnum;
2940 save_lnum = cctx->ctx_lnum;
2941 cctx->ctx_lnum = start_ctx_lnum;
2942
2943 status = check_ppconst_bool(ppconst);
2944 if (status != FAIL)
2945 {
2946 // Use the last ppconst if possible.
2947 if (ppconst->pp_used > 0)
2948 {
2949 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2950 int is_true = tv2bool(tv);
2951
2952 if ((is_true && opchar == '|')
2953 || (!is_true && opchar == '&'))
2954 {
2955 // For "false && expr" and "true || expr" the "expr"
2956 // does not need to be evaluated.
2957 cctx->ctx_skip = SKIP_YES;
2958 clear_tv(tv);
2959 tv->v_type = VAR_BOOL;
2960 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2961 }
2962 else
2963 {
2964 // For "true && expr" and "false || expr" only "expr"
2965 // needs to be evaluated.
2966 --ppconst->pp_used;
2967 jump_when = JUMP_NEVER;
2968 }
2969 }
2970 else
2971 {
2972 // Every part must evaluate to a bool.
2973 status = bool_on_stack(cctx);
2974 }
2975 }
2976 if (status != FAIL)
2977 status = ga_grow(&end_ga, 1);
2978 cctx->ctx_lnum = save_lnum;
2979 if (status == FAIL)
2980 {
2981 ga_clear(&end_ga);
2982 return FAIL;
2983 }
2984
2985 if (jump_when != JUMP_NEVER)
2986 {
2987 if (cctx->ctx_skip != SKIP_YES)
2988 {
2989 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2990 ++end_ga.ga_len;
2991 }
2992 generate_JUMP(cctx, jump_when, 0);
2993 }
2994
2995 // eval the next expression
2996 SOURCING_LNUM = save_sourcing_lnum;
2997 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2998 {
2999 ga_clear(&end_ga);
3000 return FAIL;
3001 }
3002
3003 const_used = ppconst->pp_used;
3004 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3005 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3006 {
3007 ga_clear(&end_ga);
3008 return FAIL;
3009 }
3010
3011 // "0 || 1" results in true, "1 && 0" results in false.
3012 if (ppconst->pp_used == const_used + 1)
3013 {
3014 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3015
3016 if (tv->v_type == VAR_NUMBER
3017 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3018 {
3019 tv->vval.v_number = tv->vval.v_number == 1
3020 ? VVAL_TRUE : VVAL_FALSE;
3021 tv->v_type = VAR_BOOL;
3022 }
3023 }
3024
3025 p = may_peek_next_line(cctx, *arg, &next);
3026 }
3027
3028 if (check_ppconst_bool(ppconst) == FAIL)
3029 {
3030 ga_clear(&end_ga);
3031 return FAIL;
3032 }
3033
3034 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3035 // Every part must evaluate to a bool.
3036 if (bool_on_stack(cctx) == FAIL)
3037 {
3038 ga_clear(&end_ga);
3039 return FAIL;
3040 }
3041
3042 if (end_ga.ga_len > 0)
3043 {
3044 // Fill in the end label in all jumps.
3045 generate_ppconst(cctx, ppconst);
3046 while (end_ga.ga_len > 0)
3047 {
3048 isn_T *isn;
3049
3050 --end_ga.ga_len;
3051 isn = ((isn_T *)instr->ga_data)
3052 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3053 isn->isn_arg.jump.jump_where = instr->ga_len;
3054 }
3055 }
3056 ga_clear(&end_ga);
3057
3058 cctx->ctx_skip = save_skip;
3059 }
3060
3061 return OK;
3062}
3063
3064/*
3065 * expr4a && expr4a && expr4a logical AND
3066 *
3067 * Produces instructions:
3068 * EVAL expr4a Push result of "expr4a"
3069 * COND2BOOL convert to bool if needed
3070 * JUMP_IF_COND_FALSE end
3071 * EVAL expr4b Push result of "expr4b"
3072 * JUMP_IF_COND_FALSE end
3073 * EVAL expr4c Push result of "expr4c"
3074 * end:
3075 */
3076 static int
3077compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3078{
3079 int ppconst_used = ppconst->pp_used;
3080
3081 // get the first variable
3082 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3083 return FAIL;
3084
3085 // || and && work almost the same
3086 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3087}
3088
3089/*
3090 * expr3a || expr3b || expr3c logical OR
3091 *
3092 * Produces instructions:
3093 * EVAL expr3a Push result of "expr3a"
3094 * COND2BOOL convert to bool if needed
3095 * JUMP_IF_COND_TRUE end
3096 * EVAL expr3b Push result of "expr3b"
3097 * JUMP_IF_COND_TRUE end
3098 * EVAL expr3c Push result of "expr3c"
3099 * end:
3100 */
3101 static int
3102compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3103{
3104 int ppconst_used = ppconst->pp_used;
3105
3106 // eval the first expression
3107 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3108 return FAIL;
3109
3110 // || and && work almost the same
3111 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3112}
3113
3114/*
3115 * Toplevel expression: expr2 ? expr1a : expr1b
3116 * Produces instructions:
3117 * EVAL expr2 Push result of "expr2"
3118 * JUMP_IF_FALSE alt jump if false
3119 * EVAL expr1a
3120 * JUMP_ALWAYS end
3121 * alt: EVAL expr1b
3122 * end:
3123 *
3124 * Toplevel expression: expr2 ?? expr1
3125 * Produces instructions:
3126 * EVAL expr2 Push result of "expr2"
3127 * JUMP_AND_KEEP_IF_TRUE end jump if true
3128 * EVAL expr1
3129 * end:
3130 */
3131 int
3132compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3133{
3134 char_u *p;
3135 int ppconst_used = ppconst->pp_used;
3136 char_u *next;
3137
3138 // Ignore all kinds of errors when not producing code.
3139 if (cctx->ctx_skip == SKIP_YES)
3140 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003141 int prev_did_emsg = did_emsg;
3142
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003143 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003144 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003145 }
3146
3147 // Evaluate the first expression.
3148 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3149 return FAIL;
3150
3151 p = may_peek_next_line(cctx, *arg, &next);
3152 if (*p == '?')
3153 {
3154 int op_falsy = p[1] == '?';
3155 garray_T *instr = &cctx->ctx_instr;
3156 garray_T *stack = &cctx->ctx_type_stack;
3157 int alt_idx = instr->ga_len;
3158 int end_idx = 0;
3159 isn_T *isn;
3160 type_T *type1 = NULL;
3161 int has_const_expr = FALSE;
3162 int const_value = FALSE;
3163 int save_skip = cctx->ctx_skip;
3164
3165 if (next != NULL)
3166 {
3167 *arg = next_line_from_context(cctx, TRUE);
3168 p = skipwhite(*arg);
3169 }
3170
3171 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3172 {
3173 semsg(_(e_white_space_required_before_and_after_str_at_str),
3174 op_falsy ? "??" : "?", p);
3175 return FAIL;
3176 }
3177
3178 if (ppconst->pp_used == ppconst_used + 1)
3179 {
3180 // the condition is a constant, we know whether the ? or the :
3181 // expression is to be evaluated.
3182 has_const_expr = TRUE;
3183 if (op_falsy)
3184 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3185 else
3186 {
3187 int error = FALSE;
3188
3189 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3190 &error);
3191 if (error)
3192 return FAIL;
3193 }
3194 cctx->ctx_skip = save_skip == SKIP_YES ||
3195 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3196
3197 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3198 // "left ?? right" and "left" is truthy: produce "left"
3199 generate_ppconst(cctx, ppconst);
3200 else
3201 {
3202 clear_tv(&ppconst->pp_tv[ppconst_used]);
3203 --ppconst->pp_used;
3204 }
3205 }
3206 else
3207 {
3208 generate_ppconst(cctx, ppconst);
3209 if (op_falsy)
3210 end_idx = instr->ga_len;
3211 generate_JUMP(cctx, op_falsy
3212 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3213 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003214 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003215 }
3216
3217 // evaluate the second expression; any type is accepted
3218 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3219 return FAIL;
3220 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3221 return FAIL;
3222
3223 if (!has_const_expr)
3224 {
3225 generate_ppconst(cctx, ppconst);
3226
3227 if (!op_falsy)
3228 {
3229 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003230 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003231 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003232
3233 end_idx = instr->ga_len;
3234 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3235
3236 // jump here from JUMP_IF_FALSE
3237 isn = ((isn_T *)instr->ga_data) + alt_idx;
3238 isn->isn_arg.jump.jump_where = instr->ga_len;
3239 }
3240 }
3241
3242 if (!op_falsy)
3243 {
3244 // Check for the ":".
3245 p = may_peek_next_line(cctx, *arg, &next);
3246 if (*p != ':')
3247 {
3248 emsg(_(e_missing_colon_after_questionmark));
3249 return FAIL;
3250 }
3251 if (next != NULL)
3252 {
3253 *arg = next_line_from_context(cctx, TRUE);
3254 p = skipwhite(*arg);
3255 }
3256
3257 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3258 {
3259 semsg(_(e_white_space_required_before_and_after_str_at_str),
3260 ":", p);
3261 return FAIL;
3262 }
3263
3264 // evaluate the third expression
3265 if (has_const_expr)
3266 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3267 ? SKIP_YES : SKIP_NOT;
3268 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3269 return FAIL;
3270 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3271 return FAIL;
3272 }
3273
3274 if (!has_const_expr)
3275 {
3276 type_T **typep;
3277
3278 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003279 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003280
3281 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003282 typep = &((((type2_T *)stack->ga_data)
3283 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003284 common_type(type1, *typep, typep, cctx->ctx_type_list);
3285
3286 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3287 isn = ((isn_T *)instr->ga_data) + end_idx;
3288 isn->isn_arg.jump.jump_where = instr->ga_len;
3289 }
3290
3291 cctx->ctx_skip = save_skip;
3292 }
3293 return OK;
3294}
3295
3296/*
3297 * Toplevel expression.
3298 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3299 * Returns OK or FAIL.
3300 */
3301 int
3302compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3303{
3304 ppconst_T ppconst;
3305
3306 CLEAR_FIELD(ppconst);
3307 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3308 {
3309 clear_ppconst(&ppconst);
3310 return FAIL;
3311 }
3312 if (is_const != NULL)
3313 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3314 if (generate_ppconst(cctx, &ppconst) == FAIL)
3315 return FAIL;
3316 return OK;
3317}
3318
3319/*
3320 * Toplevel expression.
3321 */
3322 int
3323compile_expr0(char_u **arg, cctx_T *cctx)
3324{
3325 return compile_expr0_ext(arg, cctx, NULL);
3326}
3327
3328
3329#endif // defined(FEAT_EVAL)