blob: 8cd095c1a796c1ea2ea9d8ee462316ab7cc656f3 [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Kota Kato948a3892022-08-16 16:09:59 +010011 * vim9expr.c: Dealing with compiled function expressions
Bram Moolenaardc7c3662021-12-20 15:04:29 +000012 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
Bram Moolenaard02dce22022-01-18 17:43:04 +000024// flag passed from compile_subscript() to compile_load_scriptvar()
25static int paren_follows_after_expr = 0;
26
Bram Moolenaardc7c3662021-12-20 15:04:29 +000027/*
28 * Generate code for any ppconst entries.
29 */
30 int
31generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
32{
33 int i;
34 int ret = OK;
35 int save_skip = cctx->ctx_skip;
36
37 cctx->ctx_skip = SKIP_NOT;
38 for (i = 0; i < ppconst->pp_used; ++i)
39 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
40 ret = FAIL;
41 ppconst->pp_used = 0;
42 cctx->ctx_skip = save_skip;
43 return ret;
44}
45
46/*
47 * Check that the last item of "ppconst" is a bool, if there is an item.
48 */
49 static int
50check_ppconst_bool(ppconst_T *ppconst)
51{
52 if (ppconst->pp_used > 0)
53 {
54 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
55 where_T where = WHERE_INIT;
56
57 return check_typval_type(&t_bool, tv, where);
58 }
59 return OK;
60}
61
62/*
63 * Clear ppconst constants. Used when failing.
64 */
65 void
66clear_ppconst(ppconst_T *ppconst)
67{
68 int i;
69
70 for (i = 0; i < ppconst->pp_used; ++i)
71 clear_tv(&ppconst->pp_tv[i]);
72 ppconst->pp_used = 0;
73}
74
75/*
76 * Compile getting a member from a list/dict/string/blob. Stack has the
77 * indexable value and the index or the two indexes of a slice.
78 * "keeping_dict" is used for dict[func](arg) to pass dict to func.
79 */
80 int
81compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
82{
Bram Moolenaar078a4612022-01-04 15:17:03 +000083 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084 garray_T *stack = &cctx->ctx_type_stack;
85 vartype_T vartype;
86 type_T *idxtype;
87
88 // We can index a list, dict and blob. If we don't know the type
89 // we can use the index value type. If we still don't know use an "ANY"
90 // instruction.
Bram Moolenaar078a4612022-01-04 15:17:03 +000091 // TODO: what about the decl type?
92 typep = (((type2_T *)stack->ga_data) + stack->ga_len - (is_slice ? 3 : 2));
93 vartype = typep->type_curr->tt_type;
94 idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000095 // If the index is a string, the variable must be a Dict.
Bram Moolenaar078a4612022-01-04 15:17:03 +000096 if ((typep->type_curr == &t_any || typep->type_curr == &t_unknown)
97 && idxtype == &t_string)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000098 vartype = VAR_DICT;
99 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
100 {
101 if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
102 return FAIL;
103 if (is_slice)
104 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000105 idxtype = get_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000106 if (need_type(idxtype, &t_number, -2, 0, cctx,
107 FALSE, FALSE) == FAIL)
108 return FAIL;
109 }
110 }
111
112 if (vartype == VAR_DICT)
113 {
114 if (is_slice)
115 {
116 emsg(_(e_cannot_slice_dictionary));
117 return FAIL;
118 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000119 if (typep->type_curr->tt_type == VAR_DICT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000120 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000121 typep->type_curr = typep->type_curr->tt_member;
122 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000123 // empty dict was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000124 typep->type_curr = &t_any;
125 if (typep->type_decl->tt_type == VAR_DICT)
126 {
127 typep->type_decl = typep->type_decl->tt_member;
128 if (typep->type_decl == &t_unknown)
129 // empty dict was used
130 typep->type_decl = &t_any;
131 }
132 else
133 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000134 }
135 else
136 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000137 if (need_type(typep->type_curr, &t_dict_any, -2, 0, cctx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000138 FALSE, FALSE) == FAIL)
139 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000140 typep->type_curr = &t_any;
141 typep->type_decl = &t_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000142 }
Bram Moolenaard0132f42022-05-12 11:05:40 +0100143 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL
144 || generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000145 return FAIL;
146 if (keeping_dict != NULL)
147 *keeping_dict = TRUE;
148 }
149 else if (vartype == VAR_STRING)
150 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000151 typep->type_curr = &t_string;
152 typep->type_decl = &t_string;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000153 if ((is_slice
154 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
155 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
156 return FAIL;
157 }
158 else if (vartype == VAR_BLOB)
159 {
160 if (is_slice)
161 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000162 typep->type_curr = &t_blob;
163 typep->type_decl = &t_blob;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000164 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
165 return FAIL;
166 }
167 else
168 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000169 typep->type_curr = &t_number;
170 typep->type_decl = &t_number;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000171 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
172 return FAIL;
173 }
174 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000175 else if (vartype == VAR_LIST || typep->type_curr == &t_any
176 || typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000177 {
178 if (is_slice)
179 {
180 if (generate_instr_drop(cctx,
181 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
182 2) == FAIL)
183 return FAIL;
Bram Moolenaar5f4ef5f2022-02-06 18:36:53 +0000184 // a copy is made so the member type is no longer declared
185 if (typep->type_decl->tt_type == VAR_LIST)
186 typep->type_decl = &t_list_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000187 }
188 else
189 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000190 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000191 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000192 typep->type_curr = typep->type_curr->tt_member;
193 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000194 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000195 typep->type_curr = &t_any;
196 if (typep->type_decl->tt_type == VAR_LIST)
197 {
198 typep->type_decl = typep->type_decl->tt_member;
199 if (typep->type_decl == &t_unknown)
200 // empty list was used
201 typep->type_decl = &t_any;
202 }
203 else
204 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000205 }
206 if (generate_instr_drop(cctx,
207 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
208 == FAIL)
209 return FAIL;
210 }
211 }
212 else
213 {
214 switch (vartype)
215 {
216 case VAR_FUNC:
217 case VAR_PARTIAL:
218 emsg(_(e_cannot_index_a_funcref));
219 break;
220 case VAR_BOOL:
221 case VAR_SPECIAL:
222 case VAR_JOB:
223 case VAR_CHANNEL:
224 case VAR_INSTR:
225 case VAR_UNKNOWN:
226 case VAR_ANY:
227 case VAR_VOID:
228 emsg(_(e_cannot_index_special_variable));
229 break;
230 default:
231 emsg(_(e_string_list_dict_or_blob_required));
232 }
233 return FAIL;
234 }
235 return OK;
236}
237
238/*
239 * Generate an instruction to load script-local variable "name", without the
240 * leading "s:".
241 * Also finds imported variables.
242 */
243 int
244compile_load_scriptvar(
245 cctx_T *cctx,
246 char_u *name, // variable NUL terminated
247 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100248 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000249{
250 scriptitem_T *si;
251 int idx;
252 imported_T *import;
253
254 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
255 return FAIL;
256 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000257 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000258 if (idx >= 0)
259 {
260 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
261
262 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
263 current_sctx.sc_sid, idx, sv->sv_type);
264 return OK;
265 }
266
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000267 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000268 if (import != NULL)
269 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000270 char_u *p = skipwhite(*end);
271 char_u *exp_name;
272 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100273 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000274 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000275 int done = FALSE;
276 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000277
278 // Need to lookup the member.
279 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000280 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000281 semsg(_(e_expected_dot_after_name_str), start);
282 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000283 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000284 ++p;
285 if (VIM_ISWHITE(*p))
286 {
287 emsg(_(e_no_white_space_allowed_after_dot));
288 return FAIL;
289 }
290
291 // isolate one name
292 exp_name = p;
293 while (eval_isnamec(*p))
294 ++p;
295 cc = *p;
296 *p = NUL;
297
Bram Moolenaard041f422022-01-12 19:54:00 +0000298 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100299 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
300 // "import autoload './dir/script.vim'" or
301 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100302 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100303
304 if (res == OK)
305 {
306 if (si->sn_autoload_prefix != NULL
307 && si->sn_state == SN_STATE_NOT_LOADED)
308 {
309 char_u *auto_name =
310 concat_str(si->sn_autoload_prefix, exp_name);
311
312 // autoload script must be loaded later, access by the autoload
313 // name. If a '(' follows it must be a function. Otherwise we
314 // don't know, it can be "script.Func".
315 if (cc == '(' || paren_follows_after_expr)
316 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any);
317 else
318 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
319 vim_free(auto_name);
320 done = TRUE;
321 }
322 else if (si->sn_import_autoload
323 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100324 {
325 // If a '(' follows it must be a function. Otherwise we don't
326 // know, it can be "script.Func".
327 if (cc == '(' || paren_follows_after_expr)
328 {
329 char_u sid_name[MAX_FUNC_NAME_LEN];
330
331 func_name_with_sid(exp_name, import->imp_sid, sid_name);
332 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any);
333 }
334 else
335 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
336 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100337 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100338 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100339 else
340 {
341 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
342 cctx, NULL, TRUE);
343 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100344 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100345
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000346 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000347 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000348 if (done)
349 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000350
351 if (idx < 0)
352 {
353 if (ufunc != NULL)
354 {
355 // function call or function reference
356 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL);
357 return OK;
358 }
359 return FAIL;
360 }
361
362 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
363 import->imp_sid,
364 idx,
365 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000366 return OK;
367 }
368
Bram Moolenaard0132f42022-05-12 11:05:40 +0100369 // Can only get here if we know "name" is a script variable and not in a
370 // Vim9 script (variable is not in sn_var_vals): old style script.
371 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000372 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000373}
374
375 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000376generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000377{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000378 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000379 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000380
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000381 // Reject a global non-autoload function found without the "g:" prefix.
382 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000383 return FAIL;
384
385 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000386 compile_type = get_compile_type(ufunc);
387 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000388 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000389 return FAIL;
390 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
391}
392
393/*
394 * Compile a variable name into a load instruction.
395 * "end" points to just after the name.
396 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
397 * When "error" is FALSE do not give an error when not found.
398 */
399 int
400compile_load(
401 char_u **arg,
402 char_u *end_arg,
403 cctx_T *cctx,
404 int is_expr,
405 int error)
406{
407 type_T *type;
408 char_u *name = NULL;
409 char_u *end = end_arg;
410 int res = FAIL;
411 int prev_called_emsg = called_emsg;
412
413 if (*(*arg + 1) == ':')
414 {
415 if (end <= *arg + 2)
416 {
417 isntype_T isn_type;
418
419 // load dictionary of namespace
420 switch (**arg)
421 {
422 case 'g': isn_type = ISN_LOADGDICT; break;
423 case 'w': isn_type = ISN_LOADWDICT; break;
424 case 't': isn_type = ISN_LOADTDICT; break;
425 case 'b': isn_type = ISN_LOADBDICT; break;
426 default:
427 semsg(_(e_namespace_not_supported_str), *arg);
428 goto theend;
429 }
430 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
431 goto theend;
432 res = OK;
433 }
434 else
435 {
436 isntype_T isn_type = ISN_DROP;
437
438 // load namespaced variable
439 name = vim_strnsave(*arg + 2, end - (*arg + 2));
440 if (name == NULL)
441 return FAIL;
442
443 switch (**arg)
444 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100445 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000446 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000447 case 's': if (current_script_is_vim9())
448 {
449 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
450 *arg);
451 vim_free(name);
452 return FAIL;
453 }
Kota Kato948a3892022-08-16 16:09:59 +0100454 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000455 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000456 else
457 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100458 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000459 break;
460 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
461 {
462 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000463 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000464 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000465 else
466 isn_type = ISN_LOADG;
467 }
468 else
469 {
470 isn_type = ISN_LOADAUTO;
471 vim_free(name);
472 name = vim_strnsave(*arg, end - *arg);
473 if (name == NULL)
474 return FAIL;
475 }
476 break;
477 case 'w': isn_type = ISN_LOADW; break;
478 case 't': isn_type = ISN_LOADT; break;
479 case 'b': isn_type = ISN_LOADB; break;
480 default: // cannot happen, just in case
481 semsg(_(e_namespace_not_supported_str), *arg);
482 goto theend;
483 }
484 if (isn_type != ISN_DROP)
485 {
486 // Global, Buffer-local, Window-local and Tabpage-local
487 // variables can be defined later, thus we don't check if it
488 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000489 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000490 }
491 }
492 }
493 else
494 {
495 size_t len = end - *arg;
496 int idx;
497 int gen_load = FALSE;
498 int gen_load_outer = 0;
499
500 name = vim_strnsave(*arg, end - *arg);
501 if (name == NULL)
502 return FAIL;
503
504 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
505 {
506 script_autoload(name, FALSE);
507 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
508 }
509 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
510 == OK)
511 {
512 if (gen_load_outer == 0)
513 gen_load = TRUE;
514 }
515 else
516 {
517 lvar_T lvar;
518
519 if (lookup_local(*arg, len, &lvar, cctx) == OK)
520 {
521 type = lvar.lv_type;
522 idx = lvar.lv_idx;
523 if (lvar.lv_from_outer != 0)
524 gen_load_outer = lvar.lv_from_outer;
525 else
526 gen_load = TRUE;
527 }
528 else
529 {
530 // "var" can be script-local even without using "s:" if it
531 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000532 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000533 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100534 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000535
536 // When evaluating an expression and the name starts with an
537 // uppercase letter it can be a user defined function.
538 // generate_funcref() will fail if the function can't be found.
539 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000540 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000541 }
542 }
543 if (gen_load)
544 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
545 if (gen_load_outer > 0)
546 {
547 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
548 cctx->ctx_outer_used = TRUE;
549 }
550 }
551
552 *arg = end;
553
554theend:
555 if (res == FAIL && error && called_emsg == prev_called_emsg)
556 semsg(_(e_variable_not_found_str), name);
557 vim_free(name);
558 return res;
559}
560
561/*
562 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100563 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000564 * Returns FAIL if compilation fails.
565 */
566 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100567compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000568{
LemonBoyf3b48952022-05-05 13:53:03 +0100569 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000570 garray_T save_ga = cctx->ctx_instr;
571 int expr_res;
572 int trailing_error;
573 int instr_count;
574 isn_T *instr = NULL;
575
576 // Remove the string type from the stack.
577 --cctx->ctx_type_stack.ga_len;
578
579 // Temporarily reset the list of instructions so that the jump labels are
580 // correct.
581 cctx->ctx_instr.ga_len = 0;
582 cctx->ctx_instr.ga_maxlen = 0;
583 cctx->ctx_instr.ga_data = NULL;
584 expr_res = compile_expr0(&s, cctx);
585 s = skipwhite(s);
586 trailing_error = *s != NUL;
587
588 if (expr_res == FAIL || trailing_error
589 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
590 {
591 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000592 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000593 clear_instr_ga(&cctx->ctx_instr);
594 cctx->ctx_instr = save_ga;
595 ++cctx->ctx_type_stack.ga_len;
596 return FAIL;
597 }
598
599 // Move the generated instructions into the ISN_INSTR instruction, then
600 // restore the list of instructions.
601 instr_count = cctx->ctx_instr.ga_len;
602 instr = cctx->ctx_instr.ga_data;
603 instr[instr_count].isn_type = ISN_FINISH;
604
605 cctx->ctx_instr = save_ga;
606 vim_free(isn->isn_arg.string);
607 isn->isn_type = ISN_INSTR;
608 isn->isn_arg.instr = instr;
609 return OK;
610}
611
612/*
LemonBoyf3b48952022-05-05 13:53:03 +0100613 * List of special functions for "compile_arguments".
614 */
615typedef enum {
616 CA_NOT_SPECIAL,
617 CA_SEARCHPAIR, // {skip} in searchpair() and searchpairpos()
618 CA_SUBSTITUTE, // {sub} in substitute(), when prefixed with \=
619} ca_special_T;
620
621/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000622 * Compile the argument expressions.
623 * "arg" points to just after the "(" and is advanced to after the ")"
624 */
625 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100626compile_arguments(
627 char_u **arg,
628 cctx_T *cctx,
629 int *argcount,
630 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000631{
632 char_u *p = *arg;
633 char_u *whitep = *arg;
634 int must_end = FALSE;
635 int instr_count;
636
637 for (;;)
638 {
639 if (may_get_next_line(whitep, &p, cctx) == FAIL)
640 goto failret;
641 if (*p == ')')
642 {
643 *arg = p + 1;
644 return OK;
645 }
646 if (must_end)
647 {
648 semsg(_(e_missing_comma_before_argument_str), p);
649 return FAIL;
650 }
651
652 instr_count = cctx->ctx_instr.ga_len;
653 if (compile_expr0(&p, cctx) == FAIL)
654 return FAIL;
655 ++*argcount;
656
LemonBoyf3b48952022-05-05 13:53:03 +0100657 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000658 && cctx->ctx_instr.ga_len == instr_count + 1)
659 {
660 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
661
662 // {skip} argument of searchpair() can be compiled if not empty
663 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100664 compile_string(isn, cctx, 0);
665 }
666 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
667 && cctx->ctx_instr.ga_len == instr_count + 1)
668 {
669 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
670
671 // {sub} argument of substitute() can be compiled if it starts
672 // with \=
673 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
674 && isn->isn_arg.string[1] == '=')
675 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000676 }
677
678 if (*p != ',' && *skipwhite(p) == ',')
679 {
680 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
681 p = skipwhite(p);
682 }
683 if (*p == ',')
684 {
685 ++p;
686 if (*p != NUL && !VIM_ISWHITE(*p))
687 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
688 }
689 else
690 must_end = TRUE;
691 whitep = p;
692 p = skipwhite(p);
693 }
694failret:
695 emsg(_(e_missing_closing_paren));
696 return FAIL;
697}
698
699/*
700 * Compile a function call: name(arg1, arg2)
701 * "arg" points to "name", "arg + varlen" to the "(".
702 * "argcount_init" is 1 for "value->method()"
703 * Instructions:
704 * EVAL arg1
705 * EVAL arg2
706 * BCALL / DCALL / UCALL
707 */
708 static int
709compile_call(
710 char_u **arg,
711 size_t varlen,
712 cctx_T *cctx,
713 ppconst_T *ppconst,
714 int argcount_init)
715{
716 char_u *name = *arg;
717 char_u *p;
718 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100719 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000720 char_u fname_buf[FLEN_FIXED + 1];
721 char_u *tofree = NULL;
722 int error = FCERR_NONE;
723 ufunc_T *ufunc = NULL;
724 int res = FAIL;
725 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000726 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100727 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000728 imported_T *import;
729
730 if (varlen >= sizeof(namebuf))
731 {
732 semsg(_(e_name_too_long_str), name);
733 return FAIL;
734 }
735 vim_strncpy(namebuf, *arg, varlen);
736
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000737 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000738 if (import != NULL)
739 {
740 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
741 return FAIL;
742 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000743
744 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100745 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100747 if ((varlen == 3
748 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000749 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
750 {
751 char_u *s = skipwhite(*arg + varlen + 1);
752 typval_T argvars[2];
753 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100754 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000755
756 argvars[0].v_type = VAR_UNKNOWN;
757 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100758 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000759 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100760 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000761 s = skipwhite(s);
762 if (*s == ')' && argvars[0].v_type == VAR_STRING
763 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
764 || !is_has))
765 {
766 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
767
768 *arg = s + 1;
769 argvars[1].v_type = VAR_UNKNOWN;
770 tv->v_type = VAR_NUMBER;
771 tv->vval.v_number = 0;
772 if (is_has)
773 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100774 else if (is_len)
775 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000776 else
777 f_exists(argvars, tv);
778 clear_tv(&argvars[0]);
779 ++ppconst->pp_used;
780 return OK;
781 }
782 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100783 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000784 {
785 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
786 return FAIL;
787 }
788 }
789
790 if (generate_ppconst(cctx, ppconst) == FAIL)
791 return FAIL;
792
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000793 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
794
795 // We handle the "skip" argument of searchpair() and searchpairpos()
796 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100797 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
798 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
799 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
800 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
801 special_fn = CA_SEARCHPAIR;
802 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
803 special_fn = CA_SUBSTITUTE;
804 else
805 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000806
807 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100808 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000809 goto theend;
810
811 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
812 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
813 {
814 int idx;
815
816 // builtin function
817 idx = find_internal_func(name);
818 if (idx >= 0)
819 {
820 if (STRCMP(name, "flatten") == 0)
821 {
822 emsg(_(e_cannot_use_flatten_in_vim9_script));
823 goto theend;
824 }
825
826 if (STRCMP(name, "add") == 0 && argcount == 2)
827 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000828 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000829
830 // add() can be compiled to instructions if we know the type
831 if (type->tt_type == VAR_LIST)
832 {
833 // inline "add(list, item)" so that the type can be checked
834 res = generate_LISTAPPEND(cctx);
835 idx = -1;
836 }
837 else if (type->tt_type == VAR_BLOB)
838 {
839 // inline "add(blob, nr)" so that the type can be checked
840 res = generate_BLOBAPPEND(cctx);
841 idx = -1;
842 }
843 }
844
845 if (idx >= 0)
846 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
847 }
848 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100849 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000850 goto theend;
851 }
852
Bram Moolenaar62aec932022-01-29 21:45:34 +0000853 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
854
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000855 // An argument or local variable can be a function reference, this
856 // overrules a function name.
857 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
858 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
859 {
860 // If we can find the function by name generate the right call.
861 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000862 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000863 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000864 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000865 if (!func_is_global(ufunc))
866 {
867 res = generate_CALL(cctx, ufunc, argcount);
868 goto theend;
869 }
870 if (!has_g_namespace
871 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
872 {
873 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100874 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000875 goto theend;
876 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000877 }
878 }
879
880 // If the name is a variable, load it and use PCALL.
881 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000882 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000884 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000885 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
886 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000887 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000888
889 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
890 goto theend;
891 }
892
893 // If we can find a global function by name generate the right call.
894 if (ufunc != NULL)
895 {
896 res = generate_CALL(cctx, ufunc, argcount);
897 goto theend;
898 }
899
900 // A global function may be defined only later. Need to figure out at
901 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000902 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000903 res = generate_UCALL(cctx, name, argcount);
904 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100905 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000906
907theend:
908 vim_free(tofree);
909 return res;
910}
911
912// like NAMESPACE_CHAR but with 'a' and 'l'.
913#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
914
915/*
916 * Find the end of a variable or function name. Unlike find_name_end() this
917 * does not recognize magic braces.
918 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
919 * Return a pointer to just after the name. Equal to "arg" if there is no
920 * valid name.
921 */
922 char_u *
923to_name_end(char_u *arg, int use_namespace)
924{
925 char_u *p;
926
927 // Quick check for valid starting character.
928 if (!eval_isnamec1(*arg))
929 return arg;
930
931 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
932 // Include a namespace such as "s:var" and "v:var". But "n:" is not
933 // and can be used in slice "[n:]".
934 if (*p == ':' && (p != arg + 1
935 || !use_namespace
936 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
937 break;
938 return p;
939}
940
941/*
942 * Like to_name_end() but also skip over a list or dict constant.
943 * Also accept "<SNR>123_Func".
944 * This intentionally does not handle line continuation.
945 */
946 char_u *
947to_name_const_end(char_u *arg)
948{
949 char_u *p = arg;
950 typval_T rettv;
951
952 if (STRNCMP(p, "<SNR>", 5) == 0)
953 p = skipdigits(p + 5);
954 p = to_name_end(p, TRUE);
955 if (p == arg && *arg == '[')
956 {
957
958 // Can be "[1, 2, 3]->Func()".
959 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
960 p = arg;
961 }
962 return p;
963}
964
965/*
966 * parse a list: [expr, expr]
967 * "*arg" points to the '['.
968 * ppconst->pp_is_const is set if all items are a constant.
969 */
970 static int
971compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
972{
973 char_u *p = skipwhite(*arg + 1);
974 char_u *whitep = *arg + 1;
975 int count = 0;
976 int is_const;
977 int is_all_const = TRUE; // reset when non-const encountered
978
979 for (;;)
980 {
981 if (may_get_next_line(whitep, &p, cctx) == FAIL)
982 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000983 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000984 return FAIL;
985 }
986 if (*p == ',')
987 {
988 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
989 return FAIL;
990 }
991 if (*p == ']')
992 {
993 ++p;
994 break;
995 }
996 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
997 return FAIL;
998 if (!is_const)
999 is_all_const = FALSE;
1000 ++count;
1001 if (*p == ',')
1002 {
1003 ++p;
1004 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1005 {
1006 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1007 return FAIL;
1008 }
1009 }
1010 whitep = p;
1011 p = skipwhite(p);
1012 }
1013 *arg = p;
1014
1015 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001016 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001017}
1018
1019/*
1020 * Parse a lambda: "(arg, arg) => expr"
1021 * "*arg" points to the '('.
1022 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1023 */
1024 static int
1025compile_lambda(char_u **arg, cctx_T *cctx)
1026{
1027 int r;
1028 typval_T rettv;
1029 ufunc_T *ufunc;
1030 evalarg_T evalarg;
1031
1032 init_evalarg(&evalarg);
1033 evalarg.eval_flags = EVAL_EVALUATE;
1034 evalarg.eval_cctx = cctx;
1035
1036 // Get the funcref in "rettv".
1037 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1038 if (r != OK)
1039 {
1040 clear_evalarg(&evalarg, NULL);
1041 return r;
1042 }
1043
1044 // "rettv" will now be a partial referencing the function.
1045 ufunc = rettv.vval.v_partial->pt_func;
1046 ++ufunc->uf_refcount;
1047 clear_tv(&rettv);
1048
1049 // Compile it here to get the return type. The return type is optional,
1050 // when it's missing use t_unknown. This is recognized in
1051 // compile_return().
1052 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1053 ufunc->uf_ret_type = &t_unknown;
1054 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1055
1056 // When the outer function is compiled for profiling or debugging, the
1057 // lambda may be called without profiling or debugging. Compile it here in
1058 // the right context.
1059 if (cctx->ctx_compile_type == CT_DEBUG
1060#ifdef FEAT_PROFILE
1061 || cctx->ctx_compile_type == CT_PROFILE
1062#endif
1063 )
1064 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1065
Bram Moolenaar139575d2022-03-15 19:29:30 +00001066 // if the outer function is not compiled for debugging or profiling, this
1067 // one might be
1068 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001069 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001070 compiletype_T compile_type = get_compile_type(ufunc);
1071
1072 if (compile_type != CT_NONE)
1073 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001074 }
1075
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001076 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1077 // "*arg" may point into it. Point into the original line to avoid a
1078 // dangling pointer.
1079 if (evalarg.eval_using_cmdline)
1080 {
1081 garray_T *gap = &evalarg.eval_tofree_ga;
1082 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1083
1084 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1085 + off;
1086 }
1087
1088 clear_evalarg(&evalarg, NULL);
1089
1090 if (ufunc->uf_def_status == UF_COMPILED)
1091 {
1092 // The return type will now be known.
1093 set_function_type(ufunc);
1094
1095 // The function reference count will be 1. When the ISN_FUNCREF
1096 // instruction is deleted the reference count is decremented and the
1097 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001098 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001099 }
1100
1101 func_ptr_unref(ufunc);
1102 return FAIL;
1103}
1104
1105/*
1106 * Get a lambda and compile it. Uses Vim9 syntax.
1107 */
1108 int
1109get_lambda_tv_and_compile(
1110 char_u **arg,
1111 typval_T *rettv,
1112 int types_optional,
1113 evalarg_T *evalarg)
1114{
1115 int r;
1116 ufunc_T *ufunc;
1117 int save_sc_version = current_sctx.sc_version;
1118
1119 // Get the funcref in "rettv".
1120 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1121 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1122 current_sctx.sc_version = save_sc_version;
1123 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001124 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001125
1126 // "rettv" will now be a partial referencing the function.
1127 ufunc = rettv->vval.v_partial->pt_func;
1128
1129 // Compile it here to get the return type. The return type is optional,
1130 // when it's missing use t_unknown. This is recognized in
1131 // compile_return().
1132 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1133 ufunc->uf_ret_type = &t_unknown;
1134 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1135
1136 if (ufunc->uf_def_status == UF_COMPILED)
1137 {
1138 // The return type will now be known.
1139 set_function_type(ufunc);
1140 return OK;
1141 }
1142 clear_tv(rettv);
1143 return FAIL;
1144}
1145
1146/*
1147 * parse a dict: {key: val, [key]: val}
1148 * "*arg" points to the '{'.
1149 * ppconst->pp_is_const is set if all item values are a constant.
1150 */
1151 static int
1152compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1153{
1154 garray_T *instr = &cctx->ctx_instr;
1155 int count = 0;
1156 dict_T *d = dict_alloc();
1157 dictitem_T *item;
1158 char_u *whitep = *arg + 1;
1159 char_u *p;
1160 int is_const;
1161 int is_all_const = TRUE; // reset when non-const encountered
1162
1163 if (d == NULL)
1164 return FAIL;
1165 if (generate_ppconst(cctx, ppconst) == FAIL)
1166 return FAIL;
1167 for (;;)
1168 {
1169 char_u *key = NULL;
1170
1171 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1172 {
1173 *arg = NULL;
1174 goto failret;
1175 }
1176
1177 if (**arg == '}')
1178 break;
1179
1180 if (**arg == '[')
1181 {
1182 isn_T *isn;
1183
1184 // {[expr]: value} uses an evaluated key.
1185 *arg = skipwhite(*arg + 1);
1186 if (compile_expr0(arg, cctx) == FAIL)
1187 return FAIL;
1188 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1189 if (isn->isn_type == ISN_PUSHNR)
1190 {
1191 char buf[NUMBUFLEN];
1192
1193 // Convert to string at compile time.
1194 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1195 isn->isn_type = ISN_PUSHS;
1196 isn->isn_arg.string = vim_strsave((char_u *)buf);
1197 }
1198 if (isn->isn_type == ISN_PUSHS)
1199 key = isn->isn_arg.string;
1200 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1201 return FAIL;
1202 *arg = skipwhite(*arg);
1203 if (**arg != ']')
1204 {
1205 emsg(_(e_missing_matching_bracket_after_dict_key));
1206 return FAIL;
1207 }
1208 ++*arg;
1209 }
1210 else
1211 {
1212 // {"name": value},
1213 // {'name': value},
1214 // {name: value} use "name" as a literal key
1215 key = get_literal_key(arg);
1216 if (key == NULL)
1217 return FAIL;
1218 if (generate_PUSHS(cctx, &key) == FAIL)
1219 return FAIL;
1220 }
1221
1222 // Check for duplicate keys, if using string keys.
1223 if (key != NULL)
1224 {
1225 item = dict_find(d, key, -1);
1226 if (item != NULL)
1227 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001228 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001229 goto failret;
1230 }
1231 item = dictitem_alloc(key);
1232 if (item != NULL)
1233 {
1234 item->di_tv.v_type = VAR_UNKNOWN;
1235 item->di_tv.v_lock = 0;
1236 if (dict_add(d, item) == FAIL)
1237 dictitem_free(item);
1238 }
1239 }
1240
1241 if (**arg != ':')
1242 {
1243 if (*skipwhite(*arg) == ':')
1244 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1245 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001246 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001247 return FAIL;
1248 }
1249 whitep = *arg + 1;
1250 if (!IS_WHITE_OR_NUL(*whitep))
1251 {
1252 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1253 return FAIL;
1254 }
1255
1256 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1257 {
1258 *arg = NULL;
1259 goto failret;
1260 }
1261
1262 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1263 return FAIL;
1264 if (!is_const)
1265 is_all_const = FALSE;
1266 ++count;
1267
1268 whitep = *arg;
1269 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1270 {
1271 *arg = NULL;
1272 goto failret;
1273 }
1274 if (**arg == '}')
1275 break;
1276 if (**arg != ',')
1277 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001278 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001279 goto failret;
1280 }
1281 if (IS_WHITE_OR_NUL(*whitep))
1282 {
1283 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1284 return FAIL;
1285 }
1286 whitep = *arg + 1;
1287 if (!IS_WHITE_OR_NUL(*whitep))
1288 {
1289 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1290 return FAIL;
1291 }
1292 *arg = skipwhite(whitep);
1293 }
1294
1295 *arg = *arg + 1;
1296
1297 // Allow for following comment, after at least one space.
1298 p = skipwhite(*arg);
1299 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1300 *arg += STRLEN(*arg);
1301
1302 dict_unref(d);
1303 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001304 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001305
1306failret:
1307 if (*arg == NULL)
1308 {
1309 semsg(_(e_missing_dict_end), _("[end of lines]"));
1310 *arg = (char_u *)"";
1311 }
1312 dict_unref(d);
1313 return FAIL;
1314}
1315
1316/*
1317 * Compile "&option".
1318 */
1319 static int
1320compile_get_option(char_u **arg, cctx_T *cctx)
1321{
1322 typval_T rettv;
1323 char_u *start = *arg;
1324 int ret;
1325
1326 // parse the option and get the current value to get the type.
1327 rettv.v_type = VAR_UNKNOWN;
1328 ret = eval_option(arg, &rettv, TRUE);
1329 if (ret == OK)
1330 {
1331 // include the '&' in the name, eval_option() expects it.
1332 char_u *name = vim_strnsave(start, *arg - start);
1333 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1334 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1335
1336 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1337 vim_free(name);
1338 }
1339 clear_tv(&rettv);
1340
1341 return ret;
1342}
1343
1344/*
1345 * Compile "$VAR".
1346 */
1347 static int
1348compile_get_env(char_u **arg, cctx_T *cctx)
1349{
1350 char_u *start = *arg;
1351 int len;
1352 int ret;
1353 char_u *name;
1354
1355 ++*arg;
1356 len = get_env_len(arg);
1357 if (len == 0)
1358 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001359 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001360 return FAIL;
1361 }
1362
1363 // include the '$' in the name, eval_env_var() expects it.
1364 name = vim_strnsave(start, len + 1);
1365 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1366 vim_free(name);
1367 return ret;
1368}
1369
1370/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001371 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001372 */
1373 static int
1374compile_interp_string(char_u **arg, cctx_T *cctx)
1375{
1376 typval_T tv;
1377 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001378 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001379 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001380 int count = 0;
1381 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001382
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001383 // *arg is on the '$' character, move it to the first string character.
1384 ++*arg;
1385 quote = **arg;
1386 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001387
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001388 for (;;)
1389 {
1390 // Get the string up to the matching quote or to a single '{'.
1391 // "arg" is advanced to either the quote or the '{'.
1392 if (quote == '"')
1393 ret = eval_string(arg, &tv, evaluate, TRUE);
1394 else
1395 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1396 if (ret == FAIL)
1397 break;
1398 if (evaluate)
1399 {
1400 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1401 || (**arg != '{' && count == 0))
1402 {
1403 // generate non-empty string or empty string if it's the only
1404 // one
1405 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1406 return FAIL;
1407 tv.vval.v_string = NULL; // don't free it now
1408 ++count;
1409 }
1410 clear_tv(&tv);
1411 }
1412
1413 if (**arg != '{')
1414 {
1415 // found terminating quote
1416 ++*arg;
1417 break;
1418 }
1419
1420 p = compile_one_expr_in_str(*arg, cctx);
1421 if (p == NULL)
1422 {
1423 ret = FAIL;
1424 break;
1425 }
1426 ++count;
1427 *arg = p;
1428 }
LemonBoy2eaef102022-05-06 13:14:50 +01001429
1430 if (ret == FAIL || !evaluate)
1431 return ret;
1432
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001433 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1434 if (count > 1)
1435 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001436
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001437 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001438}
1439
1440/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001441 * Compile "@r".
1442 */
1443 static int
1444compile_get_register(char_u **arg, cctx_T *cctx)
1445{
1446 int ret;
1447
1448 ++*arg;
1449 if (**arg == NUL)
1450 {
1451 semsg(_(e_syntax_error_at_str), *arg - 1);
1452 return FAIL;
1453 }
1454 if (!valid_yank_reg(**arg, FALSE))
1455 {
1456 emsg_invreg(**arg);
1457 return FAIL;
1458 }
1459 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1460 ++*arg;
1461 return ret;
1462}
1463
1464/*
1465 * Apply leading '!', '-' and '+' to constant "rettv".
1466 * When "numeric_only" is TRUE do not apply '!'.
1467 */
1468 static int
1469apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1470{
1471 char_u *p = *end;
1472
1473 // this works from end to start
1474 while (p > start)
1475 {
1476 --p;
1477 if (*p == '-' || *p == '+')
1478 {
1479 // only '-' has an effect, for '+' we only check the type
1480#ifdef FEAT_FLOAT
1481 if (rettv->v_type == VAR_FLOAT)
1482 {
1483 if (*p == '-')
1484 rettv->vval.v_float = -rettv->vval.v_float;
1485 }
1486 else
1487#endif
1488 {
1489 varnumber_T val;
1490 int error = FALSE;
1491
1492 // tv_get_number_chk() accepts a string, but we don't want that
1493 // here
1494 if (check_not_string(rettv) == FAIL)
1495 return FAIL;
1496 val = tv_get_number_chk(rettv, &error);
1497 clear_tv(rettv);
1498 if (error)
1499 return FAIL;
1500 if (*p == '-')
1501 val = -val;
1502 rettv->v_type = VAR_NUMBER;
1503 rettv->vval.v_number = val;
1504 }
1505 }
1506 else if (numeric_only)
1507 {
1508 ++p;
1509 break;
1510 }
1511 else if (*p == '!')
1512 {
1513 int v = tv2bool(rettv);
1514
1515 // '!' is permissive in the type.
1516 clear_tv(rettv);
1517 rettv->v_type = VAR_BOOL;
1518 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1519 }
1520 }
1521 *end = p;
1522 return OK;
1523}
1524
1525/*
1526 * Recognize v: variables that are constants and set "rettv".
1527 */
1528 static void
1529get_vim_constant(char_u **arg, typval_T *rettv)
1530{
1531 if (STRNCMP(*arg, "v:true", 6) == 0)
1532 {
1533 rettv->v_type = VAR_BOOL;
1534 rettv->vval.v_number = VVAL_TRUE;
1535 *arg += 6;
1536 }
1537 else if (STRNCMP(*arg, "v:false", 7) == 0)
1538 {
1539 rettv->v_type = VAR_BOOL;
1540 rettv->vval.v_number = VVAL_FALSE;
1541 *arg += 7;
1542 }
1543 else if (STRNCMP(*arg, "v:null", 6) == 0)
1544 {
1545 rettv->v_type = VAR_SPECIAL;
1546 rettv->vval.v_number = VVAL_NULL;
1547 *arg += 6;
1548 }
1549 else if (STRNCMP(*arg, "v:none", 6) == 0)
1550 {
1551 rettv->v_type = VAR_SPECIAL;
1552 rettv->vval.v_number = VVAL_NONE;
1553 *arg += 6;
1554 }
1555}
1556
1557 exprtype_T
1558get_compare_type(char_u *p, int *len, int *type_is)
1559{
1560 exprtype_T type = EXPR_UNKNOWN;
1561 int i;
1562
1563 switch (p[0])
1564 {
1565 case '=': if (p[1] == '=')
1566 type = EXPR_EQUAL;
1567 else if (p[1] == '~')
1568 type = EXPR_MATCH;
1569 break;
1570 case '!': if (p[1] == '=')
1571 type = EXPR_NEQUAL;
1572 else if (p[1] == '~')
1573 type = EXPR_NOMATCH;
1574 break;
1575 case '>': if (p[1] != '=')
1576 {
1577 type = EXPR_GREATER;
1578 *len = 1;
1579 }
1580 else
1581 type = EXPR_GEQUAL;
1582 break;
1583 case '<': if (p[1] != '=')
1584 {
1585 type = EXPR_SMALLER;
1586 *len = 1;
1587 }
1588 else
1589 type = EXPR_SEQUAL;
1590 break;
1591 case 'i': if (p[1] == 's')
1592 {
1593 // "is" and "isnot"; but not a prefix of a name
1594 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1595 *len = 5;
1596 i = p[*len];
1597 if (!isalnum(i) && i != '_')
1598 {
1599 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1600 *type_is = TRUE;
1601 }
1602 }
1603 break;
1604 }
1605 return type;
1606}
1607
1608/*
1609 * Skip over an expression, ignoring most errors.
1610 */
1611 void
1612skip_expr_cctx(char_u **arg, cctx_T *cctx)
1613{
1614 evalarg_T evalarg;
1615
1616 init_evalarg(&evalarg);
1617 evalarg.eval_cctx = cctx;
1618 skip_expr(arg, &evalarg);
1619 clear_evalarg(&evalarg, NULL);
1620}
1621
1622/*
1623 * Check that the top of the type stack has a type that can be used as a
1624 * condition. Give an error and return FAIL if not.
1625 */
1626 int
1627bool_on_stack(cctx_T *cctx)
1628{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001629 type_T *type;
1630
Bram Moolenaar078a4612022-01-04 15:17:03 +00001631 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001632 if (type == &t_bool)
1633 return OK;
1634
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001635 if (type == &t_any
1636 || type == &t_unknown
1637 || type == &t_number
1638 || type == &t_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001639 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1640 // This requires a runtime type check.
1641 return generate_COND2BOOL(cctx);
1642
1643 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1644}
1645
1646/*
1647 * Give the "white on both sides" error, taking the operator from "p[len]".
1648 */
1649 void
1650error_white_both(char_u *op, int len)
1651{
1652 char_u buf[10];
1653
1654 vim_strncpy(buf, op, len);
1655 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1656}
1657
1658/*
1659 * Compile code to apply '-', '+' and '!'.
1660 * When "numeric_only" is TRUE do not apply '!'.
1661 */
1662 static int
1663compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1664{
1665 char_u *p = *end;
1666
1667 // this works from end to start
1668 while (p > start)
1669 {
1670 --p;
1671 while (VIM_ISWHITE(*p))
1672 --p;
1673 if (*p == '-' || *p == '+')
1674 {
1675 int negate = *p == '-';
1676 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001677 type_T *type;
1678
Bram Moolenaar078a4612022-01-04 15:17:03 +00001679 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001680 if (type != &t_float && need_type(type, &t_number,
1681 -1, 0, cctx, FALSE, FALSE) == FAIL)
1682 return FAIL;
1683
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001684 // only '-' has an effect, for '+' we only check the type
1685 if (negate)
1686 {
1687 isn = generate_instr(cctx, ISN_NEGATENR);
1688 if (isn == NULL)
1689 return FAIL;
1690 }
1691 }
1692 else if (numeric_only)
1693 {
1694 ++p;
1695 break;
1696 }
1697 else
1698 {
1699 int invert = *p == '!';
1700
1701 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1702 {
1703 if (p[-1] == '!')
1704 invert = !invert;
1705 --p;
1706 }
1707 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1708 return FAIL;
1709 }
1710 }
1711 *end = p;
1712 return OK;
1713}
1714
1715/*
1716 * Compile "(expression)": recursive!
1717 * Return FAIL/OK.
1718 */
1719 static int
1720compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1721{
1722 int ret;
1723 char_u *p = *arg + 1;
1724
1725 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1726 return FAIL;
1727 if (ppconst->pp_used <= PPSIZE - 10)
1728 {
1729 ret = compile_expr1(arg, cctx, ppconst);
1730 }
1731 else
1732 {
1733 // Not enough space in ppconst, flush constants.
1734 if (generate_ppconst(cctx, ppconst) == FAIL)
1735 return FAIL;
1736 ret = compile_expr0(arg, cctx);
1737 }
1738 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1739 return FAIL;
1740 if (**arg == ')')
1741 ++*arg;
1742 else if (ret == OK)
1743 {
1744 emsg(_(e_missing_closing_paren));
1745 ret = FAIL;
1746 }
1747 return ret;
1748}
1749
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001750static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001751
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001752/*
1753 * Compile whatever comes after "name" or "name()".
1754 * Advances "*arg" only when something was recognized.
1755 */
1756 static int
1757compile_subscript(
1758 char_u **arg,
1759 cctx_T *cctx,
1760 char_u *start_leader,
1761 char_u **end_leader,
1762 ppconst_T *ppconst)
1763{
1764 char_u *name_start = *end_leader;
1765 int keeping_dict = FALSE;
1766
1767 for (;;)
1768 {
1769 char_u *p = skipwhite(*arg);
1770
1771 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1772 {
1773 char_u *next = peek_next_line_from_context(cctx);
1774
1775 // If a following line starts with "->{" or "->X" advance to that
1776 // line, so that a line break before "->" is allowed.
1777 // Also if a following line starts with ".x".
1778 if (next != NULL &&
1779 ((next[0] == '-' && next[1] == '>'
1780 && (next[2] == '{'
1781 || ASCII_ISALPHA(*skipwhite(next + 2))))
1782 || (next[0] == '.' && eval_isdictc(next[1]))))
1783 {
1784 next = next_line_from_context(cctx, TRUE);
1785 if (next == NULL)
1786 return FAIL;
1787 *arg = next;
1788 p = skipwhite(*arg);
1789 }
1790 }
1791
1792 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1793 // is not a function call.
1794 if (**arg == '(')
1795 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001796 type_T *type;
1797 int argcount = 0;
1798
1799 if (generate_ppconst(cctx, ppconst) == FAIL)
1800 return FAIL;
1801 ppconst->pp_is_const = FALSE;
1802
1803 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001804 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001805
1806 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001807 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001808 return FAIL;
1809 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1810 return FAIL;
1811 if (keeping_dict)
1812 {
1813 keeping_dict = FALSE;
1814 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1815 return FAIL;
1816 }
1817 }
1818 else if (*p == '-' && p[1] == '>')
1819 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001820 char_u *pstart = p;
1821 int alt;
1822 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001823
Bram Moolenaarc7349932022-01-16 20:59:39 +00001824 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001825 if (generate_ppconst(cctx, ppconst) == FAIL)
1826 return FAIL;
1827 ppconst->pp_is_const = FALSE;
1828
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001829 // Apply the '!', '-' and '+' first:
1830 // -1.0->func() works like (-1.0)->func()
1831 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1832 return FAIL;
1833
1834 p += 2;
1835 *arg = skipwhite(p);
1836 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001837
1838 // Three alternatives handled here:
1839 // 1. "base->name(" only a name, use compile_call()
1840 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1841 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001842 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001843 alt = 2;
1844 else
1845 {
1846 // alternative 1 or 3
1847 p = *arg;
1848 if (!eval_isnamec1(*p))
1849 {
1850 semsg(_(e_trailing_characters_str), pstart);
1851 return FAIL;
1852 }
1853 if (ASCII_ISALPHA(*p) && p[1] == ':')
1854 p += 2;
1855 for ( ; eval_isnamec(*p); ++p)
1856 ;
1857 if (*p == '(')
1858 {
1859 // alternative 1
1860 alt = 1;
1861 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1862 return FAIL;
1863 }
1864 else
1865 {
1866 // Must be alternative 3, find the "(". Only works within
1867 // one line.
1868 alt = 3;
1869 paren = vim_strchr(p, '(');
1870 if (paren == NULL)
1871 {
1872 semsg(_(e_missing_parenthesis_str), *arg);
1873 return FAIL;
1874 }
1875 }
1876 }
1877
1878 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001879 {
1880 int argcount = 1;
1881 garray_T *stack = &cctx->ctx_type_stack;
1882 int type_idx_start = stack->ga_len;
1883 type_T *type;
1884 int expr_isn_start = cctx->ctx_instr.ga_len;
1885 int expr_isn_end;
1886 int arg_isn_count;
1887
Bram Moolenaarc7349932022-01-16 20:59:39 +00001888 if (alt == 2)
1889 {
1890 // Funcref call: list->(Refs[2])(arg)
1891 // or lambda: list->((arg) => expr)(arg)
1892 //
1893 // Fist compile the function expression.
1894 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1895 return FAIL;
1896 }
1897 else
1898 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001899 int fail;
1900 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001901 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001902
Bram Moolenaarc7349932022-01-16 20:59:39 +00001903 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001904
1905 // instead of using LOADG for "import.Func" use PUSHFUNC
1906 ++paren_follows_after_expr;
1907
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001908 // do not look in the next line
1909 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001910
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001911 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001912 || *skipwhite(*arg) != NUL;
1913 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001914 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001915 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001916
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001917 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001918 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001919 if (did_emsg == prev_did_emsg)
1920 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001921 return FAIL;
1922 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001923 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001924
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001925 // Compile the arguments.
1926 if (**arg != '(')
1927 {
1928 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001929 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001930 else
1931 semsg(_(e_missing_parenthesis_str), *arg);
1932 return FAIL;
1933 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001934
1935 // Remember the next instruction index, where the instructions
1936 // for arguments are being written.
1937 expr_isn_end = cctx->ctx_instr.ga_len;
1938
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001939 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001940 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
1941 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001942 return FAIL;
1943
1944 // Move the instructions for the arguments to before the
1945 // instructions of the expression and move the type of the
1946 // expression after the argument types. This is what ISN_PCALL
1947 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001948 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1949 if (arg_isn_count > 0)
1950 {
1951 int expr_isn_count = expr_isn_end - expr_isn_start;
1952 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001953 type_T *decl_type;
1954 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001955
1956 if (isn == NULL)
1957 return FAIL;
1958 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1959 + expr_isn_start,
1960 sizeof(isn_T) * expr_isn_count);
1961 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1962 + expr_isn_start,
1963 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1964 sizeof(isn_T) * arg_isn_count);
1965 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1966 + expr_isn_start + arg_isn_count,
1967 isn, sizeof(isn_T) * expr_isn_count);
1968 vim_free(isn);
1969
Bram Moolenaar078a4612022-01-04 15:17:03 +00001970 typep = ((type2_T *)stack->ga_data) + type_idx_start;
1971 type = typep->type_curr;
1972 decl_type = typep->type_decl;
1973 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
1974 ((type2_T *)stack->ga_data) + type_idx_start + 1,
1975 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001976 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00001977 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
1978 typep->type_curr = type;
1979 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001980 }
1981
Bram Moolenaar078a4612022-01-04 15:17:03 +00001982 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001983 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
1984 return FAIL;
1985 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001986
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001987 if (keeping_dict)
1988 {
1989 keeping_dict = FALSE;
1990 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1991 return FAIL;
1992 }
1993 }
1994 else if (**arg == '[')
1995 {
1996 int is_slice = FALSE;
1997
1998 // list index: list[123]
1999 // dict member: dict[key]
2000 // string index: text[123]
2001 // blob index: blob[123]
2002 if (generate_ppconst(cctx, ppconst) == FAIL)
2003 return FAIL;
2004 ppconst->pp_is_const = FALSE;
2005
2006 ++p;
2007 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2008 return FAIL;
2009 if (**arg == ':')
2010 {
2011 // missing first index is equal to zero
2012 generate_PUSHNR(cctx, 0);
2013 }
2014 else
2015 {
2016 if (compile_expr0(arg, cctx) == FAIL)
2017 return FAIL;
2018 if (**arg == ':')
2019 {
2020 semsg(_(e_white_space_required_before_and_after_str_at_str),
2021 ":", *arg);
2022 return FAIL;
2023 }
2024 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2025 return FAIL;
2026 *arg = skipwhite(*arg);
2027 }
2028 if (**arg == ':')
2029 {
2030 is_slice = TRUE;
2031 ++*arg;
2032 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2033 {
2034 semsg(_(e_white_space_required_before_and_after_str_at_str),
2035 ":", *arg);
2036 return FAIL;
2037 }
2038 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2039 return FAIL;
2040 if (**arg == ']')
2041 // missing second index is equal to end of string
2042 generate_PUSHNR(cctx, -1);
2043 else
2044 {
2045 if (compile_expr0(arg, cctx) == FAIL)
2046 return FAIL;
2047 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2048 return FAIL;
2049 *arg = skipwhite(*arg);
2050 }
2051 }
2052
2053 if (**arg != ']')
2054 {
2055 emsg(_(e_missing_closing_square_brace));
2056 return FAIL;
2057 }
2058 *arg = *arg + 1;
2059
2060 if (keeping_dict)
2061 {
2062 keeping_dict = FALSE;
2063 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2064 return FAIL;
2065 }
2066 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2067 return FAIL;
2068 }
2069 else if (*p == '.' && p[1] != '.')
2070 {
2071 // dictionary member: dict.name
2072 if (generate_ppconst(cctx, ppconst) == FAIL)
2073 return FAIL;
2074 ppconst->pp_is_const = FALSE;
2075
2076 *arg = p + 1;
2077 if (IS_WHITE_OR_NUL(**arg))
2078 {
2079 emsg(_(e_missing_name_after_dot));
2080 return FAIL;
2081 }
2082 p = *arg;
2083 if (eval_isdictc(*p))
2084 while (eval_isnamec(*p))
2085 MB_PTR_ADV(p);
2086 if (p == *arg)
2087 {
2088 semsg(_(e_syntax_error_at_str), *arg);
2089 return FAIL;
2090 }
2091 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2092 return FAIL;
2093 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2094 return FAIL;
2095 keeping_dict = TRUE;
2096 *arg = p;
2097 }
2098 else
2099 break;
2100 }
2101
2102 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2103 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002104 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2105 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002106 return FAIL;
2107
2108 return OK;
2109}
2110
2111/*
2112 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2113 * "arg" is advanced until after the expression, skipping white space.
2114 *
2115 * If the value is a constant "ppconst->pp_used" will be non-zero.
2116 * Before instructions are generated, any values in "ppconst" will generated.
2117 *
2118 * This is the compiling equivalent of eval1(), eval2(), etc.
2119 */
2120
2121/*
2122 * number number constant
2123 * 0zFFFFFFFF Blob constant
2124 * "string" string constant
2125 * 'string' literal string constant
2126 * &option-name option value
2127 * @r register contents
2128 * identifier variable value
2129 * function() function call
2130 * $VAR environment variable
2131 * (expression) nested expression
2132 * [expr, expr] List
2133 * {key: val, [key]: val} Dictionary
2134 *
2135 * Also handle:
2136 * ! in front logical NOT
2137 * - in front unary minus
2138 * + in front unary plus (ignored)
2139 * trailing (arg) funcref/partial call
2140 * trailing [] subscript in String or List
2141 * trailing .name entry in Dictionary
2142 * trailing ->name() method call
2143 */
2144 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002145compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002146 char_u **arg,
2147 cctx_T *cctx,
2148 ppconst_T *ppconst)
2149{
2150 char_u *start_leader, *end_leader;
2151 int ret = OK;
2152 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2153 int used_before = ppconst->pp_used;
2154
2155 ppconst->pp_is_const = FALSE;
2156
2157 /*
2158 * Skip '!', '-' and '+' characters. They are handled later.
2159 */
2160 start_leader = *arg;
2161 if (eval_leader(arg, TRUE) == FAIL)
2162 return FAIL;
2163 end_leader = *arg;
2164
2165 rettv->v_type = VAR_UNKNOWN;
2166 switch (**arg)
2167 {
2168 /*
2169 * Number constant.
2170 */
2171 case '0': // also for blob starting with 0z
2172 case '1':
2173 case '2':
2174 case '3':
2175 case '4':
2176 case '5':
2177 case '6':
2178 case '7':
2179 case '8':
2180 case '9':
2181 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2182 return FAIL;
2183 // Apply "-" and "+" just before the number now, right to
2184 // left. Matters especially when "->" follows. Stops at
2185 // '!'.
2186 if (apply_leader(rettv, TRUE,
2187 start_leader, &end_leader) == FAIL)
2188 {
2189 clear_tv(rettv);
2190 return FAIL;
2191 }
2192 break;
2193
2194 /*
2195 * String constant: "string".
2196 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002197 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002198 return FAIL;
2199 break;
2200
2201 /*
2202 * Literal string constant: 'str''ing'.
2203 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002204 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002205 return FAIL;
2206 break;
2207
2208 /*
2209 * Constant Vim variable.
2210 */
2211 case 'v': get_vim_constant(arg, rettv);
2212 ret = NOTDONE;
2213 break;
2214
2215 /*
2216 * "true" constant
2217 */
2218 case 't': if (STRNCMP(*arg, "true", 4) == 0
2219 && !eval_isnamec((*arg)[4]))
2220 {
2221 *arg += 4;
2222 rettv->v_type = VAR_BOOL;
2223 rettv->vval.v_number = VVAL_TRUE;
2224 }
2225 else
2226 ret = NOTDONE;
2227 break;
2228
2229 /*
2230 * "false" constant
2231 */
2232 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2233 && !eval_isnamec((*arg)[5]))
2234 {
2235 *arg += 5;
2236 rettv->v_type = VAR_BOOL;
2237 rettv->vval.v_number = VVAL_FALSE;
2238 }
2239 else
2240 ret = NOTDONE;
2241 break;
2242
2243 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002244 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002245 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002246 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002247 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002248 char_u *p = *arg + 4;
2249 int len;
2250
2251 for (len = 0; eval_isnamec(p[len]); ++len)
2252 ;
2253 ret = handle_predefined(*arg, len + 4, rettv);
2254 if (ret == FAIL)
2255 ret = NOTDONE;
2256 else
2257 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002258 }
2259 else
2260 ret = NOTDONE;
2261 break;
2262
2263 /*
2264 * List: [expr, expr]
2265 */
2266 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2267 return FAIL;
2268 ret = compile_list(arg, cctx, ppconst);
2269 break;
2270
2271 /*
2272 * Dictionary: {'key': val, 'key': val}
2273 */
2274 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2275 return FAIL;
2276 ret = compile_dict(arg, cctx, ppconst);
2277 break;
2278
2279 /*
2280 * Option value: &name
2281 */
2282 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2283 return FAIL;
2284 ret = compile_get_option(arg, cctx);
2285 break;
2286
2287 /*
2288 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002289 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002290 */
2291 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2292 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002293 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2294 ret = compile_interp_string(arg, cctx);
2295 else
2296 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002297 break;
2298
2299 /*
2300 * Register contents: @r.
2301 */
2302 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2303 return FAIL;
2304 ret = compile_get_register(arg, cctx);
2305 break;
2306 /*
2307 * nested expression: (expression).
2308 * lambda: (arg, arg) => expr
2309 * funcref: (arg, arg) => { statement }
2310 */
2311 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2312 ret = compile_lambda(arg, cctx);
2313 if (ret == NOTDONE)
2314 ret = compile_parenthesis(arg, cctx, ppconst);
2315 break;
2316
2317 default: ret = NOTDONE;
2318 break;
2319 }
2320 if (ret == FAIL)
2321 return FAIL;
2322
2323 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2324 {
2325 if (cctx->ctx_skip == SKIP_YES)
2326 clear_tv(rettv);
2327 else
2328 // A constant expression can possibly be handled compile time,
2329 // return the value instead of generating code.
2330 ++ppconst->pp_used;
2331 }
2332 else if (ret == NOTDONE)
2333 {
2334 char_u *p;
2335 int r;
2336
2337 if (!eval_isnamec1(**arg))
2338 {
2339 if (!vim9_bad_comment(*arg))
2340 {
2341 if (ends_excmd(*skipwhite(*arg)))
2342 semsg(_(e_empty_expression_str), *arg);
2343 else
2344 semsg(_(e_name_expected_str), *arg);
2345 }
2346 return FAIL;
2347 }
2348
2349 // "name" or "name()"
2350 p = to_name_end(*arg, TRUE);
2351 if (p - *arg == (size_t)1 && **arg == '_')
2352 {
2353 emsg(_(e_cannot_use_underscore_here));
2354 return FAIL;
2355 }
2356
2357 if (*p == '(')
2358 {
2359 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2360 }
2361 else
2362 {
2363 if (cctx->ctx_skip != SKIP_YES
2364 && generate_ppconst(cctx, ppconst) == FAIL)
2365 return FAIL;
2366 r = compile_load(arg, p, cctx, TRUE, TRUE);
2367 }
2368 if (r == FAIL)
2369 return FAIL;
2370 }
2371
2372 // Handle following "[]", ".member", etc.
2373 // Then deal with prefixed '-', '+' and '!', if not done already.
2374 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2375 ppconst) == FAIL)
2376 return FAIL;
2377 if (ppconst->pp_used > 0)
2378 {
2379 // apply the '!', '-' and '+' before the constant
2380 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2381 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2382 return FAIL;
2383 return OK;
2384 }
2385 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2386 return FAIL;
2387 return OK;
2388}
2389
2390/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002391 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002392 */
2393 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002394compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002395{
2396 type_T *want_type = NULL;
2397
2398 // Recognize <type>
2399 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2400 {
2401 ++*arg;
2402 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2403 if (want_type == NULL)
2404 return FAIL;
2405
2406 if (**arg != '>')
2407 {
2408 if (*skipwhite(*arg) == '>')
2409 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2410 else
2411 emsg(_(e_missing_gt));
2412 return FAIL;
2413 }
2414 ++*arg;
2415 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2416 return FAIL;
2417 }
2418
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002419 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002420 return FAIL;
2421
2422 if (want_type != NULL)
2423 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002424 type_T *actual;
2425 where_T where = WHERE_INIT;
2426
2427 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002428 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002429 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002430 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002431 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2432 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002433 return FAIL;
2434 }
2435 }
2436
2437 return OK;
2438}
2439
2440/*
2441 * * number multiplication
2442 * / number division
2443 * % number modulo
2444 */
2445 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002446compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002447{
2448 char_u *op;
2449 char_u *next;
2450 int ppconst_used = ppconst->pp_used;
2451
2452 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002453 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002454 return FAIL;
2455
2456 /*
2457 * Repeat computing, until no "*", "/" or "%" is following.
2458 */
2459 for (;;)
2460 {
2461 op = may_peek_next_line(cctx, *arg, &next);
2462 if (*op != '*' && *op != '/' && *op != '%')
2463 break;
2464 if (next != NULL)
2465 {
2466 *arg = next_line_from_context(cctx, TRUE);
2467 op = skipwhite(*arg);
2468 }
2469
2470 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2471 {
2472 error_white_both(op, 1);
2473 return FAIL;
2474 }
2475 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2476 return FAIL;
2477
2478 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002479 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002480 return FAIL;
2481
2482 if (ppconst->pp_used == ppconst_used + 2
2483 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2484 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2485 {
2486 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2487 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2488 varnumber_T res = 0;
2489 int failed = FALSE;
2490
2491 // both are numbers: compute the result
2492 switch (*op)
2493 {
2494 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2495 break;
2496 case '/': res = num_divide(tv1->vval.v_number,
2497 tv2->vval.v_number, &failed);
2498 break;
2499 case '%': res = num_modulus(tv1->vval.v_number,
2500 tv2->vval.v_number, &failed);
2501 break;
2502 }
2503 if (failed)
2504 return FAIL;
2505 tv1->vval.v_number = res;
2506 --ppconst->pp_used;
2507 }
2508 else
2509 {
2510 generate_ppconst(cctx, ppconst);
2511 generate_two_op(cctx, op);
2512 }
2513 }
2514
2515 return OK;
2516}
2517
2518/*
2519 * + number addition or list/blobl concatenation
2520 * - number subtraction
2521 * .. string concatenation
2522 */
2523 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002524compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002525{
2526 char_u *op;
2527 char_u *next;
2528 int oplen;
2529 int ppconst_used = ppconst->pp_used;
2530
2531 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002532 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002533 return FAIL;
2534
2535 /*
2536 * Repeat computing, until no "+", "-" or ".." is following.
2537 */
2538 for (;;)
2539 {
2540 op = may_peek_next_line(cctx, *arg, &next);
2541 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2542 break;
2543 if (op[0] == op[1] && *op != '.' && next)
2544 // Finding "++" or "--" on the next line is a separate command.
2545 // But ".." is concatenation.
2546 break;
2547 oplen = (*op == '.' ? 2 : 1);
2548 if (next != NULL)
2549 {
2550 *arg = next_line_from_context(cctx, TRUE);
2551 op = skipwhite(*arg);
2552 }
2553
2554 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2555 {
2556 error_white_both(op, oplen);
2557 return FAIL;
2558 }
2559
2560 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2561 return FAIL;
2562
2563 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002564 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002565 return FAIL;
2566
2567 if (ppconst->pp_used == ppconst_used + 2
2568 && (*op == '.'
2569 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2570 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2571 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2572 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2573 {
2574 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2575 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2576
2577 // concat/subtract/add constant numbers
2578 if (*op == '+')
2579 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2580 else if (*op == '-')
2581 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2582 else
2583 {
2584 // concatenate constant strings
2585 char_u *s1 = tv1->vval.v_string;
2586 char_u *s2 = tv2->vval.v_string;
2587 size_t len1 = STRLEN(s1);
2588
2589 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2590 if (tv1->vval.v_string == NULL)
2591 {
2592 clear_ppconst(ppconst);
2593 return FAIL;
2594 }
2595 mch_memmove(tv1->vval.v_string, s1, len1);
2596 STRCPY(tv1->vval.v_string + len1, s2);
2597 vim_free(s1);
2598 vim_free(s2);
2599 }
2600 --ppconst->pp_used;
2601 }
2602 else
2603 {
2604 generate_ppconst(cctx, ppconst);
2605 ppconst->pp_is_const = FALSE;
2606 if (*op == '.')
2607 {
2608 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2609 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2610 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002611 if (generate_CONCAT(cctx, 2) == FAIL)
2612 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002613 }
2614 else
2615 generate_two_op(cctx, op);
2616 }
2617 }
2618
2619 return OK;
2620}
2621
2622/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002623 * expr6a >> expr6b
2624 * expr6a << expr6b
2625 *
2626 * Produces instructions:
2627 * OPNR bitwise left or right shift
2628 */
2629 static int
2630compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2631{
2632 exprtype_T type = EXPR_UNKNOWN;
2633 char_u *p;
2634 char_u *next;
2635 int len = 2;
2636 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002637 isn_T *isn;
2638
2639 // get the first variable
2640 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2641 return FAIL;
2642
2643 /*
2644 * Repeat computing, until no "+", "-" or ".." is following.
2645 */
2646 for (;;)
2647 {
2648 type = EXPR_UNKNOWN;
2649
2650 p = may_peek_next_line(cctx, *arg, &next);
2651 if (p[0] == '<' && p[1] == '<')
2652 type = EXPR_LSHIFT;
2653 else if (p[0] == '>' && p[1] == '>')
2654 type = EXPR_RSHIFT;
2655
2656 if (type == EXPR_UNKNOWN)
2657 return OK;
2658
2659 // Handle a bitwise left or right shift operator
2660 if (ppconst->pp_used == ppconst_used + 1)
2661 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002662 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002663 {
2664 // left operand should be a number
2665 emsg(_(e_bitshift_ops_must_be_number));
2666 return FAIL;
2667 }
2668 }
2669 else
2670 {
2671 type_T *t = get_type_on_stack(cctx, 0);
2672
2673 if (need_type(t, &t_number, 0, 0, cctx, FALSE, FALSE) == FAIL)
2674 {
2675 emsg(_(e_bitshift_ops_must_be_number));
2676 return FAIL;
2677 }
2678 }
2679
2680 if (next != NULL)
2681 {
2682 *arg = next_line_from_context(cctx, TRUE);
2683 p = skipwhite(*arg);
2684 }
2685
2686 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2687 {
2688 error_white_both(p, len);
2689 return FAIL;
2690 }
2691
2692 // get the second variable
2693 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2694 return FAIL;
2695
2696 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2697 return FAIL;
2698
2699 if (ppconst->pp_used == ppconst_used + 2)
2700 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002701 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2702 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2703
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002704 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002705 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2706 {
2707 // right operand should be a positive number
2708 if (tv2->v_type != VAR_NUMBER)
2709 emsg(_(e_bitshift_ops_must_be_number));
2710 else
2711 emsg(_(e_bitshift_ops_must_be_postive));
2712 return FAIL;
2713 }
2714
2715 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2716 tv1->vval.v_number = 0;
2717 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002718 tv1->vval.v_number =
2719 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002720 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002721 tv1->vval.v_number =
2722 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002723 clear_tv(tv2);
2724 --ppconst->pp_used;
2725 }
2726 else
2727 {
2728 if (need_type(get_type_on_stack(cctx, 0), &t_number, 0, 0, cctx,
2729 FALSE, FALSE) == FAIL)
2730 {
2731 emsg(_(e_bitshift_ops_must_be_number));
2732 return FAIL;
2733 }
2734
2735 generate_ppconst(cctx, ppconst);
2736
2737 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2738 if (isn == NULL)
2739 return FAIL;
2740
2741 if (isn != NULL)
2742 isn->isn_arg.op.op_type = type;
2743 }
2744 }
2745
2746 return OK;
2747}
2748
2749/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002750 * expr5a == expr5b
2751 * expr5a =~ expr5b
2752 * expr5a != expr5b
2753 * expr5a !~ expr5b
2754 * expr5a > expr5b
2755 * expr5a >= expr5b
2756 * expr5a < expr5b
2757 * expr5a <= expr5b
2758 * expr5a is expr5b
2759 * expr5a isnot expr5b
2760 *
2761 * Produces instructions:
2762 * EVAL expr5a Push result of "expr5a"
2763 * EVAL expr5b Push result of "expr5b"
2764 * COMPARE one of the compare instructions
2765 */
2766 static int
2767compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2768{
2769 exprtype_T type = EXPR_UNKNOWN;
2770 char_u *p;
2771 char_u *next;
2772 int len = 2;
2773 int type_is = FALSE;
2774 int ppconst_used = ppconst->pp_used;
2775
2776 // get the first variable
2777 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2778 return FAIL;
2779
2780 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002781
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002782 type = get_compare_type(p, &len, &type_is);
2783
2784 /*
2785 * If there is a comparative operator, use it.
2786 */
2787 if (type != EXPR_UNKNOWN)
2788 {
2789 int ic = FALSE; // Default: do not ignore case
2790
2791 if (next != NULL)
2792 {
2793 *arg = next_line_from_context(cctx, TRUE);
2794 p = skipwhite(*arg);
2795 }
2796 if (type_is && (p[len] == '?' || p[len] == '#'))
2797 {
2798 semsg(_(e_invalid_expression_str), *arg);
2799 return FAIL;
2800 }
2801 // extra question mark appended: ignore case
2802 if (p[len] == '?')
2803 {
2804 ic = TRUE;
2805 ++len;
2806 }
2807 // extra '#' appended: match case (ignored)
2808 else if (p[len] == '#')
2809 ++len;
2810 // nothing appended: match case
2811
2812 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2813 {
2814 error_white_both(p, len);
2815 return FAIL;
2816 }
2817
2818 // get the second variable
2819 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2820 return FAIL;
2821
2822 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2823 return FAIL;
2824
2825 if (ppconst->pp_used == ppconst_used + 2)
2826 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002827 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002828 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2829 int ret;
2830
2831 // Both sides are a constant, compute the result now.
2832 // First check for a valid combination of types, this is more
2833 // strict than typval_compare().
2834 if (check_compare_types(type, tv1, tv2) == FAIL)
2835 ret = FAIL;
2836 else
2837 {
2838 ret = typval_compare(tv1, tv2, type, ic);
2839 tv1->v_type = VAR_BOOL;
2840 tv1->vval.v_number = tv1->vval.v_number
2841 ? VVAL_TRUE : VVAL_FALSE;
2842 clear_tv(tv2);
2843 --ppconst->pp_used;
2844 }
2845 return ret;
2846 }
2847
2848 generate_ppconst(cctx, ppconst);
2849 return generate_COMPARE(cctx, type, ic);
2850 }
2851
2852 return OK;
2853}
2854
2855static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2856
2857/*
2858 * Compile || or &&.
2859 */
2860 static int
2861compile_and_or(
2862 char_u **arg,
2863 cctx_T *cctx,
2864 char *op,
2865 ppconst_T *ppconst,
2866 int ppconst_used UNUSED)
2867{
2868 char_u *next;
2869 char_u *p = may_peek_next_line(cctx, *arg, &next);
2870 int opchar = *op;
2871
2872 if (p[0] == opchar && p[1] == opchar)
2873 {
2874 garray_T *instr = &cctx->ctx_instr;
2875 garray_T end_ga;
2876 int save_skip = cctx->ctx_skip;
2877
2878 /*
2879 * Repeat until there is no following "||" or "&&"
2880 */
2881 ga_init2(&end_ga, sizeof(int), 10);
2882 while (p[0] == opchar && p[1] == opchar)
2883 {
2884 long start_lnum = SOURCING_LNUM;
2885 long save_sourcing_lnum;
2886 int start_ctx_lnum = cctx->ctx_lnum;
2887 int save_lnum;
2888 int const_used;
2889 int status;
2890 jumpwhen_T jump_when = opchar == '|'
2891 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2892
2893 if (next != NULL)
2894 {
2895 *arg = next_line_from_context(cctx, TRUE);
2896 p = skipwhite(*arg);
2897 }
2898
2899 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2900 {
2901 semsg(_(e_white_space_required_before_and_after_str_at_str),
2902 op, p);
2903 ga_clear(&end_ga);
2904 return FAIL;
2905 }
2906
2907 save_sourcing_lnum = SOURCING_LNUM;
2908 SOURCING_LNUM = start_lnum;
2909 save_lnum = cctx->ctx_lnum;
2910 cctx->ctx_lnum = start_ctx_lnum;
2911
2912 status = check_ppconst_bool(ppconst);
2913 if (status != FAIL)
2914 {
2915 // Use the last ppconst if possible.
2916 if (ppconst->pp_used > 0)
2917 {
2918 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2919 int is_true = tv2bool(tv);
2920
2921 if ((is_true && opchar == '|')
2922 || (!is_true && opchar == '&'))
2923 {
2924 // For "false && expr" and "true || expr" the "expr"
2925 // does not need to be evaluated.
2926 cctx->ctx_skip = SKIP_YES;
2927 clear_tv(tv);
2928 tv->v_type = VAR_BOOL;
2929 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2930 }
2931 else
2932 {
2933 // For "true && expr" and "false || expr" only "expr"
2934 // needs to be evaluated.
2935 --ppconst->pp_used;
2936 jump_when = JUMP_NEVER;
2937 }
2938 }
2939 else
2940 {
2941 // Every part must evaluate to a bool.
2942 status = bool_on_stack(cctx);
2943 }
2944 }
2945 if (status != FAIL)
2946 status = ga_grow(&end_ga, 1);
2947 cctx->ctx_lnum = save_lnum;
2948 if (status == FAIL)
2949 {
2950 ga_clear(&end_ga);
2951 return FAIL;
2952 }
2953
2954 if (jump_when != JUMP_NEVER)
2955 {
2956 if (cctx->ctx_skip != SKIP_YES)
2957 {
2958 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2959 ++end_ga.ga_len;
2960 }
2961 generate_JUMP(cctx, jump_when, 0);
2962 }
2963
2964 // eval the next expression
2965 SOURCING_LNUM = save_sourcing_lnum;
2966 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2967 {
2968 ga_clear(&end_ga);
2969 return FAIL;
2970 }
2971
2972 const_used = ppconst->pp_used;
2973 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
2974 : compile_expr4(arg, cctx, ppconst)) == FAIL)
2975 {
2976 ga_clear(&end_ga);
2977 return FAIL;
2978 }
2979
2980 // "0 || 1" results in true, "1 && 0" results in false.
2981 if (ppconst->pp_used == const_used + 1)
2982 {
2983 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2984
2985 if (tv->v_type == VAR_NUMBER
2986 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
2987 {
2988 tv->vval.v_number = tv->vval.v_number == 1
2989 ? VVAL_TRUE : VVAL_FALSE;
2990 tv->v_type = VAR_BOOL;
2991 }
2992 }
2993
2994 p = may_peek_next_line(cctx, *arg, &next);
2995 }
2996
2997 if (check_ppconst_bool(ppconst) == FAIL)
2998 {
2999 ga_clear(&end_ga);
3000 return FAIL;
3001 }
3002
3003 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3004 // Every part must evaluate to a bool.
3005 if (bool_on_stack(cctx) == FAIL)
3006 {
3007 ga_clear(&end_ga);
3008 return FAIL;
3009 }
3010
3011 if (end_ga.ga_len > 0)
3012 {
3013 // Fill in the end label in all jumps.
3014 generate_ppconst(cctx, ppconst);
3015 while (end_ga.ga_len > 0)
3016 {
3017 isn_T *isn;
3018
3019 --end_ga.ga_len;
3020 isn = ((isn_T *)instr->ga_data)
3021 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3022 isn->isn_arg.jump.jump_where = instr->ga_len;
3023 }
3024 }
3025 ga_clear(&end_ga);
3026
3027 cctx->ctx_skip = save_skip;
3028 }
3029
3030 return OK;
3031}
3032
3033/*
3034 * expr4a && expr4a && expr4a logical AND
3035 *
3036 * Produces instructions:
3037 * EVAL expr4a Push result of "expr4a"
3038 * COND2BOOL convert to bool if needed
3039 * JUMP_IF_COND_FALSE end
3040 * EVAL expr4b Push result of "expr4b"
3041 * JUMP_IF_COND_FALSE end
3042 * EVAL expr4c Push result of "expr4c"
3043 * end:
3044 */
3045 static int
3046compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3047{
3048 int ppconst_used = ppconst->pp_used;
3049
3050 // get the first variable
3051 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3052 return FAIL;
3053
3054 // || and && work almost the same
3055 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3056}
3057
3058/*
3059 * expr3a || expr3b || expr3c logical OR
3060 *
3061 * Produces instructions:
3062 * EVAL expr3a Push result of "expr3a"
3063 * COND2BOOL convert to bool if needed
3064 * JUMP_IF_COND_TRUE end
3065 * EVAL expr3b Push result of "expr3b"
3066 * JUMP_IF_COND_TRUE end
3067 * EVAL expr3c Push result of "expr3c"
3068 * end:
3069 */
3070 static int
3071compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3072{
3073 int ppconst_used = ppconst->pp_used;
3074
3075 // eval the first expression
3076 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3077 return FAIL;
3078
3079 // || and && work almost the same
3080 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3081}
3082
3083/*
3084 * Toplevel expression: expr2 ? expr1a : expr1b
3085 * Produces instructions:
3086 * EVAL expr2 Push result of "expr2"
3087 * JUMP_IF_FALSE alt jump if false
3088 * EVAL expr1a
3089 * JUMP_ALWAYS end
3090 * alt: EVAL expr1b
3091 * end:
3092 *
3093 * Toplevel expression: expr2 ?? expr1
3094 * Produces instructions:
3095 * EVAL expr2 Push result of "expr2"
3096 * JUMP_AND_KEEP_IF_TRUE end jump if true
3097 * EVAL expr1
3098 * end:
3099 */
3100 int
3101compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3102{
3103 char_u *p;
3104 int ppconst_used = ppconst->pp_used;
3105 char_u *next;
3106
3107 // Ignore all kinds of errors when not producing code.
3108 if (cctx->ctx_skip == SKIP_YES)
3109 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003110 int prev_did_emsg = did_emsg;
3111
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003112 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003113 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003114 }
3115
3116 // Evaluate the first expression.
3117 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3118 return FAIL;
3119
3120 p = may_peek_next_line(cctx, *arg, &next);
3121 if (*p == '?')
3122 {
3123 int op_falsy = p[1] == '?';
3124 garray_T *instr = &cctx->ctx_instr;
3125 garray_T *stack = &cctx->ctx_type_stack;
3126 int alt_idx = instr->ga_len;
3127 int end_idx = 0;
3128 isn_T *isn;
3129 type_T *type1 = NULL;
3130 int has_const_expr = FALSE;
3131 int const_value = FALSE;
3132 int save_skip = cctx->ctx_skip;
3133
3134 if (next != NULL)
3135 {
3136 *arg = next_line_from_context(cctx, TRUE);
3137 p = skipwhite(*arg);
3138 }
3139
3140 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3141 {
3142 semsg(_(e_white_space_required_before_and_after_str_at_str),
3143 op_falsy ? "??" : "?", p);
3144 return FAIL;
3145 }
3146
3147 if (ppconst->pp_used == ppconst_used + 1)
3148 {
3149 // the condition is a constant, we know whether the ? or the :
3150 // expression is to be evaluated.
3151 has_const_expr = TRUE;
3152 if (op_falsy)
3153 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3154 else
3155 {
3156 int error = FALSE;
3157
3158 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3159 &error);
3160 if (error)
3161 return FAIL;
3162 }
3163 cctx->ctx_skip = save_skip == SKIP_YES ||
3164 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3165
3166 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3167 // "left ?? right" and "left" is truthy: produce "left"
3168 generate_ppconst(cctx, ppconst);
3169 else
3170 {
3171 clear_tv(&ppconst->pp_tv[ppconst_used]);
3172 --ppconst->pp_used;
3173 }
3174 }
3175 else
3176 {
3177 generate_ppconst(cctx, ppconst);
3178 if (op_falsy)
3179 end_idx = instr->ga_len;
3180 generate_JUMP(cctx, op_falsy
3181 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3182 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003183 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003184 }
3185
3186 // evaluate the second expression; any type is accepted
3187 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3188 return FAIL;
3189 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3190 return FAIL;
3191
3192 if (!has_const_expr)
3193 {
3194 generate_ppconst(cctx, ppconst);
3195
3196 if (!op_falsy)
3197 {
3198 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003199 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003200 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003201
3202 end_idx = instr->ga_len;
3203 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3204
3205 // jump here from JUMP_IF_FALSE
3206 isn = ((isn_T *)instr->ga_data) + alt_idx;
3207 isn->isn_arg.jump.jump_where = instr->ga_len;
3208 }
3209 }
3210
3211 if (!op_falsy)
3212 {
3213 // Check for the ":".
3214 p = may_peek_next_line(cctx, *arg, &next);
3215 if (*p != ':')
3216 {
3217 emsg(_(e_missing_colon_after_questionmark));
3218 return FAIL;
3219 }
3220 if (next != NULL)
3221 {
3222 *arg = next_line_from_context(cctx, TRUE);
3223 p = skipwhite(*arg);
3224 }
3225
3226 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3227 {
3228 semsg(_(e_white_space_required_before_and_after_str_at_str),
3229 ":", p);
3230 return FAIL;
3231 }
3232
3233 // evaluate the third expression
3234 if (has_const_expr)
3235 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3236 ? SKIP_YES : SKIP_NOT;
3237 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3238 return FAIL;
3239 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3240 return FAIL;
3241 }
3242
3243 if (!has_const_expr)
3244 {
3245 type_T **typep;
3246
3247 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003248 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003249
3250 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003251 typep = &((((type2_T *)stack->ga_data)
3252 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003253 common_type(type1, *typep, typep, cctx->ctx_type_list);
3254
3255 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3256 isn = ((isn_T *)instr->ga_data) + end_idx;
3257 isn->isn_arg.jump.jump_where = instr->ga_len;
3258 }
3259
3260 cctx->ctx_skip = save_skip;
3261 }
3262 return OK;
3263}
3264
3265/*
3266 * Toplevel expression.
3267 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3268 * Returns OK or FAIL.
3269 */
3270 int
3271compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3272{
3273 ppconst_T ppconst;
3274
3275 CLEAR_FIELD(ppconst);
3276 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3277 {
3278 clear_ppconst(&ppconst);
3279 return FAIL;
3280 }
3281 if (is_const != NULL)
3282 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3283 if (generate_ppconst(cctx, &ppconst) == FAIL)
3284 return FAIL;
3285 return OK;
3286}
3287
3288/*
3289 * Toplevel expression.
3290 */
3291 int
3292compile_expr0(char_u **arg, cctx_T *cctx)
3293{
3294 return compile_expr0_ext(arg, cctx, NULL);
3295}
3296
3297
3298#endif // defined(FEAT_EVAL)