blob: d5bb276b7df68d23670edb7d4e57a446c4217656 [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/*
11 * vim9cmds.c: Dealing with compiled function expressions
12 */
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 }
143 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
144 return FAIL;
145 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
146 return FAIL;
147 if (keeping_dict != NULL)
148 *keeping_dict = TRUE;
149 }
150 else if (vartype == VAR_STRING)
151 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000152 typep->type_curr = &t_string;
153 typep->type_decl = &t_string;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000154 if ((is_slice
155 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
156 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
157 return FAIL;
158 }
159 else if (vartype == VAR_BLOB)
160 {
161 if (is_slice)
162 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000163 typep->type_curr = &t_blob;
164 typep->type_decl = &t_blob;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000165 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
166 return FAIL;
167 }
168 else
169 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000170 typep->type_curr = &t_number;
171 typep->type_decl = &t_number;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000172 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
173 return FAIL;
174 }
175 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000176 else if (vartype == VAR_LIST || typep->type_curr == &t_any
177 || typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000178 {
179 if (is_slice)
180 {
181 if (generate_instr_drop(cctx,
182 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
183 2) == FAIL)
184 return FAIL;
185 }
186 else
187 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000188 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000189 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000190 typep->type_curr = typep->type_curr->tt_member;
191 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000192 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000193 typep->type_curr = &t_any;
194 if (typep->type_decl->tt_type == VAR_LIST)
195 {
196 typep->type_decl = typep->type_decl->tt_member;
197 if (typep->type_decl == &t_unknown)
198 // empty list was used
199 typep->type_decl = &t_any;
200 }
201 else
202 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000203 }
204 if (generate_instr_drop(cctx,
205 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
206 == FAIL)
207 return FAIL;
208 }
209 }
210 else
211 {
212 switch (vartype)
213 {
214 case VAR_FUNC:
215 case VAR_PARTIAL:
216 emsg(_(e_cannot_index_a_funcref));
217 break;
218 case VAR_BOOL:
219 case VAR_SPECIAL:
220 case VAR_JOB:
221 case VAR_CHANNEL:
222 case VAR_INSTR:
223 case VAR_UNKNOWN:
224 case VAR_ANY:
225 case VAR_VOID:
226 emsg(_(e_cannot_index_special_variable));
227 break;
228 default:
229 emsg(_(e_string_list_dict_or_blob_required));
230 }
231 return FAIL;
232 }
233 return OK;
234}
235
236/*
237 * Generate an instruction to load script-local variable "name", without the
238 * leading "s:".
239 * Also finds imported variables.
240 */
241 int
242compile_load_scriptvar(
243 cctx_T *cctx,
244 char_u *name, // variable NUL terminated
245 char_u *start, // start of variable
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000246 char_u **end, // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000247 int error) // when TRUE may give error
248{
249 scriptitem_T *si;
250 int idx;
251 imported_T *import;
252
253 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
254 return FAIL;
255 si = SCRIPT_ITEM(current_sctx.sc_sid);
256 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
257 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
258 {
259 // variable is not in sn_var_vals: old style script.
260 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
261 &t_any);
262 }
263 if (idx >= 0)
264 {
265 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
266
267 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
268 current_sctx.sc_sid, idx, sv->sv_type);
269 return OK;
270 }
271
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000272 import = end == NULL ? NULL : find_imported(name, 0, FALSE, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000273 if (import != NULL)
274 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000275 char_u *p = skipwhite(*end);
276 char_u *exp_name;
277 int cc;
278 ufunc_T *ufunc;
279 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000280 int done = FALSE;
281 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000282
283 // Need to lookup the member.
284 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000285 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000286 semsg(_(e_expected_dot_after_name_str), start);
287 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000288 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000289 ++p;
290 if (VIM_ISWHITE(*p))
291 {
292 emsg(_(e_no_white_space_allowed_after_dot));
293 return FAIL;
294 }
295
296 // isolate one name
297 exp_name = p;
298 while (eval_isnamec(*p))
299 ++p;
300 cc = *p;
301 *p = NUL;
302
Bram Moolenaard041f422022-01-12 19:54:00 +0000303 si = SCRIPT_ITEM(import->imp_sid);
304 if (si->sn_autoload_prefix != NULL
305 && si->sn_state == SN_STATE_NOT_LOADED)
306 {
307 char_u *auto_name = concat_str(si->sn_autoload_prefix, exp_name);
308
309 // autoload script must be loaded later, access by the autoload
310 // name.
Bram Moolenaard02dce22022-01-18 17:43:04 +0000311 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaard041f422022-01-12 19:54:00 +0000312 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any);
313 else
314 res = generate_LOAD(cctx, ISN_LOADG, 0, auto_name, &t_any);
315 vim_free(auto_name);
316 done = TRUE;
317 }
318 else
319 {
320 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000321 cctx, TRUE);
Bram Moolenaard041f422022-01-12 19:54:00 +0000322 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000323 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000324 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000325 if (done)
326 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000327
328 if (idx < 0)
329 {
330 if (ufunc != NULL)
331 {
332 // function call or function reference
333 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL);
334 return OK;
335 }
336 return FAIL;
337 }
338
339 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
340 import->imp_sid,
341 idx,
342 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000343 return OK;
344 }
345
346 if (error)
347 semsg(_(e_item_not_found_str), name);
348 return FAIL;
349}
350
351 static int
352generate_funcref(cctx_T *cctx, char_u *name)
353{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000354 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000355
356 if (ufunc == NULL)
357 return FAIL;
358
359 // Need to compile any default values to get the argument types.
360 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
361 && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
362 == FAIL)
363 return FAIL;
364 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
365}
366
367/*
368 * Compile a variable name into a load instruction.
369 * "end" points to just after the name.
370 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
371 * When "error" is FALSE do not give an error when not found.
372 */
373 int
374compile_load(
375 char_u **arg,
376 char_u *end_arg,
377 cctx_T *cctx,
378 int is_expr,
379 int error)
380{
381 type_T *type;
382 char_u *name = NULL;
383 char_u *end = end_arg;
384 int res = FAIL;
385 int prev_called_emsg = called_emsg;
386
387 if (*(*arg + 1) == ':')
388 {
389 if (end <= *arg + 2)
390 {
391 isntype_T isn_type;
392
393 // load dictionary of namespace
394 switch (**arg)
395 {
396 case 'g': isn_type = ISN_LOADGDICT; break;
397 case 'w': isn_type = ISN_LOADWDICT; break;
398 case 't': isn_type = ISN_LOADTDICT; break;
399 case 'b': isn_type = ISN_LOADBDICT; break;
400 default:
401 semsg(_(e_namespace_not_supported_str), *arg);
402 goto theend;
403 }
404 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
405 goto theend;
406 res = OK;
407 }
408 else
409 {
410 isntype_T isn_type = ISN_DROP;
411
412 // load namespaced variable
413 name = vim_strnsave(*arg + 2, end - (*arg + 2));
414 if (name == NULL)
415 return FAIL;
416
417 switch (**arg)
418 {
419 case 'v': res = generate_LOADV(cctx, name, error);
420 break;
421 case 's': if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000422 && find_func(name, FALSE) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000423 res = generate_funcref(cctx, name);
424 else
425 res = compile_load_scriptvar(cctx, name,
426 NULL, &end, error);
427 break;
428 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
429 {
430 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000431 && find_func(name, FALSE) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000432 res = generate_funcref(cctx, name);
433 else
434 isn_type = ISN_LOADG;
435 }
436 else
437 {
438 isn_type = ISN_LOADAUTO;
439 vim_free(name);
440 name = vim_strnsave(*arg, end - *arg);
441 if (name == NULL)
442 return FAIL;
443 }
444 break;
445 case 'w': isn_type = ISN_LOADW; break;
446 case 't': isn_type = ISN_LOADT; break;
447 case 'b': isn_type = ISN_LOADB; break;
448 default: // cannot happen, just in case
449 semsg(_(e_namespace_not_supported_str), *arg);
450 goto theend;
451 }
452 if (isn_type != ISN_DROP)
453 {
454 // Global, Buffer-local, Window-local and Tabpage-local
455 // variables can be defined later, thus we don't check if it
456 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000457 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000458 }
459 }
460 }
461 else
462 {
463 size_t len = end - *arg;
464 int idx;
465 int gen_load = FALSE;
466 int gen_load_outer = 0;
467
468 name = vim_strnsave(*arg, end - *arg);
469 if (name == NULL)
470 return FAIL;
471
472 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
473 {
474 script_autoload(name, FALSE);
475 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
476 }
477 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
478 == OK)
479 {
480 if (gen_load_outer == 0)
481 gen_load = TRUE;
482 }
483 else
484 {
485 lvar_T lvar;
486
487 if (lookup_local(*arg, len, &lvar, cctx) == OK)
488 {
489 type = lvar.lv_type;
490 idx = lvar.lv_idx;
491 if (lvar.lv_from_outer != 0)
492 gen_load_outer = lvar.lv_from_outer;
493 else
494 gen_load = TRUE;
495 }
496 else
497 {
498 // "var" can be script-local even without using "s:" if it
499 // already exists in a Vim9 script or when it's imported.
500 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000501 || find_imported(name, 0, FALSE, cctx) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000502 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
503
504 // When evaluating an expression and the name starts with an
505 // uppercase letter it can be a user defined function.
506 // generate_funcref() will fail if the function can't be found.
507 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
508 res = generate_funcref(cctx, name);
509 }
510 }
511 if (gen_load)
512 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
513 if (gen_load_outer > 0)
514 {
515 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
516 cctx->ctx_outer_used = TRUE;
517 }
518 }
519
520 *arg = end;
521
522theend:
523 if (res == FAIL && error && called_emsg == prev_called_emsg)
524 semsg(_(e_variable_not_found_str), name);
525 vim_free(name);
526 return res;
527}
528
529/*
530 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
531 * Returns FAIL if compilation fails.
532 */
533 static int
534compile_string(isn_T *isn, cctx_T *cctx)
535{
536 char_u *s = isn->isn_arg.string;
537 garray_T save_ga = cctx->ctx_instr;
538 int expr_res;
539 int trailing_error;
540 int instr_count;
541 isn_T *instr = NULL;
542
543 // Remove the string type from the stack.
544 --cctx->ctx_type_stack.ga_len;
545
546 // Temporarily reset the list of instructions so that the jump labels are
547 // correct.
548 cctx->ctx_instr.ga_len = 0;
549 cctx->ctx_instr.ga_maxlen = 0;
550 cctx->ctx_instr.ga_data = NULL;
551 expr_res = compile_expr0(&s, cctx);
552 s = skipwhite(s);
553 trailing_error = *s != NUL;
554
555 if (expr_res == FAIL || trailing_error
556 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
557 {
558 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000559 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000560 clear_instr_ga(&cctx->ctx_instr);
561 cctx->ctx_instr = save_ga;
562 ++cctx->ctx_type_stack.ga_len;
563 return FAIL;
564 }
565
566 // Move the generated instructions into the ISN_INSTR instruction, then
567 // restore the list of instructions.
568 instr_count = cctx->ctx_instr.ga_len;
569 instr = cctx->ctx_instr.ga_data;
570 instr[instr_count].isn_type = ISN_FINISH;
571
572 cctx->ctx_instr = save_ga;
573 vim_free(isn->isn_arg.string);
574 isn->isn_type = ISN_INSTR;
575 isn->isn_arg.instr = instr;
576 return OK;
577}
578
579/*
580 * Compile the argument expressions.
581 * "arg" points to just after the "(" and is advanced to after the ")"
582 */
583 static int
584compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
585{
586 char_u *p = *arg;
587 char_u *whitep = *arg;
588 int must_end = FALSE;
589 int instr_count;
590
591 for (;;)
592 {
593 if (may_get_next_line(whitep, &p, cctx) == FAIL)
594 goto failret;
595 if (*p == ')')
596 {
597 *arg = p + 1;
598 return OK;
599 }
600 if (must_end)
601 {
602 semsg(_(e_missing_comma_before_argument_str), p);
603 return FAIL;
604 }
605
606 instr_count = cctx->ctx_instr.ga_len;
607 if (compile_expr0(&p, cctx) == FAIL)
608 return FAIL;
609 ++*argcount;
610
611 if (is_searchpair && *argcount == 5
612 && cctx->ctx_instr.ga_len == instr_count + 1)
613 {
614 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
615
616 // {skip} argument of searchpair() can be compiled if not empty
617 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
618 compile_string(isn, cctx);
619 }
620
621 if (*p != ',' && *skipwhite(p) == ',')
622 {
623 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
624 p = skipwhite(p);
625 }
626 if (*p == ',')
627 {
628 ++p;
629 if (*p != NUL && !VIM_ISWHITE(*p))
630 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
631 }
632 else
633 must_end = TRUE;
634 whitep = p;
635 p = skipwhite(p);
636 }
637failret:
638 emsg(_(e_missing_closing_paren));
639 return FAIL;
640}
641
642/*
643 * Compile a function call: name(arg1, arg2)
644 * "arg" points to "name", "arg + varlen" to the "(".
645 * "argcount_init" is 1 for "value->method()"
646 * Instructions:
647 * EVAL arg1
648 * EVAL arg2
649 * BCALL / DCALL / UCALL
650 */
651 static int
652compile_call(
653 char_u **arg,
654 size_t varlen,
655 cctx_T *cctx,
656 ppconst_T *ppconst,
657 int argcount_init)
658{
659 char_u *name = *arg;
660 char_u *p;
661 int argcount = argcount_init;
662 char_u namebuf[100];
663 char_u fname_buf[FLEN_FIXED + 1];
664 char_u *tofree = NULL;
665 int error = FCERR_NONE;
666 ufunc_T *ufunc = NULL;
667 int res = FAIL;
668 int is_autoload;
669 int is_searchpair;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000670 imported_T *import;
671
672 if (varlen >= sizeof(namebuf))
673 {
674 semsg(_(e_name_too_long_str), name);
675 return FAIL;
676 }
677 vim_strncpy(namebuf, *arg, varlen);
678
679 import = find_imported(name, varlen, FALSE, cctx);
680 if (import != NULL)
681 {
682 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
683 return FAIL;
684 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000685
686 // We can evaluate "has('name')" at compile time.
687 // We always evaluate "exists_compiled()" at compile time.
688 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
689 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
690 {
691 char_u *s = skipwhite(*arg + varlen + 1);
692 typval_T argvars[2];
693 int is_has = **arg == 'h';
694
695 argvars[0].v_type = VAR_UNKNOWN;
696 if (*s == '"')
697 (void)eval_string(&s, &argvars[0], TRUE);
698 else if (*s == '\'')
699 (void)eval_lit_string(&s, &argvars[0], TRUE);
700 s = skipwhite(s);
701 if (*s == ')' && argvars[0].v_type == VAR_STRING
702 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
703 || !is_has))
704 {
705 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
706
707 *arg = s + 1;
708 argvars[1].v_type = VAR_UNKNOWN;
709 tv->v_type = VAR_NUMBER;
710 tv->vval.v_number = 0;
711 if (is_has)
712 f_has(argvars, tv);
713 else
714 f_exists(argvars, tv);
715 clear_tv(&argvars[0]);
716 ++ppconst->pp_used;
717 return OK;
718 }
719 clear_tv(&argvars[0]);
720 if (!is_has)
721 {
722 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
723 return FAIL;
724 }
725 }
726
727 if (generate_ppconst(cctx, ppconst) == FAIL)
728 return FAIL;
729
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000730 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
731
732 // We handle the "skip" argument of searchpair() and searchpairpos()
733 // differently.
734 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
735 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
736 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
737 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
738
739 *arg = skipwhite(*arg + varlen + 1);
740 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
741 goto theend;
742
743 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
744 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
745 {
746 int idx;
747
748 // builtin function
749 idx = find_internal_func(name);
750 if (idx >= 0)
751 {
752 if (STRCMP(name, "flatten") == 0)
753 {
754 emsg(_(e_cannot_use_flatten_in_vim9_script));
755 goto theend;
756 }
757
758 if (STRCMP(name, "add") == 0 && argcount == 2)
759 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000760 type_T *type = get_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000761
762 // add() can be compiled to instructions if we know the type
763 if (type->tt_type == VAR_LIST)
764 {
765 // inline "add(list, item)" so that the type can be checked
766 res = generate_LISTAPPEND(cctx);
767 idx = -1;
768 }
769 else if (type->tt_type == VAR_BLOB)
770 {
771 // inline "add(blob, nr)" so that the type can be checked
772 res = generate_BLOBAPPEND(cctx);
773 idx = -1;
774 }
775 }
776
777 if (idx >= 0)
778 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
779 }
780 else
781 semsg(_(e_unknown_function_str), namebuf);
782 goto theend;
783 }
784
785 // An argument or local variable can be a function reference, this
786 // overrules a function name.
787 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
788 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
789 {
790 // If we can find the function by name generate the right call.
791 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000792 ufunc = find_func(name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000793 if (ufunc != NULL && !func_is_global(ufunc))
794 {
795 res = generate_CALL(cctx, ufunc, argcount);
796 goto theend;
797 }
798 }
799
800 // If the name is a variable, load it and use PCALL.
801 // Not for g:Func(), we don't know if it is a variable or not.
802 // Not for eome#Func(), it will be loaded later.
803 p = namebuf;
804 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
805 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
806 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000807 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000808
809 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
810 goto theend;
811 }
812
813 // If we can find a global function by name generate the right call.
814 if (ufunc != NULL)
815 {
816 res = generate_CALL(cctx, ufunc, argcount);
817 goto theend;
818 }
819
820 // A global function may be defined only later. Need to figure out at
821 // runtime. Also handles a FuncRef at runtime.
822 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
823 res = generate_UCALL(cctx, name, argcount);
824 else
825 semsg(_(e_unknown_function_str), namebuf);
826
827theend:
828 vim_free(tofree);
829 return res;
830}
831
832// like NAMESPACE_CHAR but with 'a' and 'l'.
833#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
834
835/*
836 * Find the end of a variable or function name. Unlike find_name_end() this
837 * does not recognize magic braces.
838 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
839 * Return a pointer to just after the name. Equal to "arg" if there is no
840 * valid name.
841 */
842 char_u *
843to_name_end(char_u *arg, int use_namespace)
844{
845 char_u *p;
846
847 // Quick check for valid starting character.
848 if (!eval_isnamec1(*arg))
849 return arg;
850
851 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
852 // Include a namespace such as "s:var" and "v:var". But "n:" is not
853 // and can be used in slice "[n:]".
854 if (*p == ':' && (p != arg + 1
855 || !use_namespace
856 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
857 break;
858 return p;
859}
860
861/*
862 * Like to_name_end() but also skip over a list or dict constant.
863 * Also accept "<SNR>123_Func".
864 * This intentionally does not handle line continuation.
865 */
866 char_u *
867to_name_const_end(char_u *arg)
868{
869 char_u *p = arg;
870 typval_T rettv;
871
872 if (STRNCMP(p, "<SNR>", 5) == 0)
873 p = skipdigits(p + 5);
874 p = to_name_end(p, TRUE);
875 if (p == arg && *arg == '[')
876 {
877
878 // Can be "[1, 2, 3]->Func()".
879 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
880 p = arg;
881 }
882 return p;
883}
884
885/*
886 * parse a list: [expr, expr]
887 * "*arg" points to the '['.
888 * ppconst->pp_is_const is set if all items are a constant.
889 */
890 static int
891compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
892{
893 char_u *p = skipwhite(*arg + 1);
894 char_u *whitep = *arg + 1;
895 int count = 0;
896 int is_const;
897 int is_all_const = TRUE; // reset when non-const encountered
898
899 for (;;)
900 {
901 if (may_get_next_line(whitep, &p, cctx) == FAIL)
902 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000903 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000904 return FAIL;
905 }
906 if (*p == ',')
907 {
908 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
909 return FAIL;
910 }
911 if (*p == ']')
912 {
913 ++p;
914 break;
915 }
916 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
917 return FAIL;
918 if (!is_const)
919 is_all_const = FALSE;
920 ++count;
921 if (*p == ',')
922 {
923 ++p;
924 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
925 {
926 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
927 return FAIL;
928 }
929 }
930 whitep = p;
931 p = skipwhite(p);
932 }
933 *arg = p;
934
935 ppconst->pp_is_const = is_all_const;
936 return generate_NEWLIST(cctx, count);
937}
938
939/*
940 * Parse a lambda: "(arg, arg) => expr"
941 * "*arg" points to the '('.
942 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
943 */
944 static int
945compile_lambda(char_u **arg, cctx_T *cctx)
946{
947 int r;
948 typval_T rettv;
949 ufunc_T *ufunc;
950 evalarg_T evalarg;
951
952 init_evalarg(&evalarg);
953 evalarg.eval_flags = EVAL_EVALUATE;
954 evalarg.eval_cctx = cctx;
955
956 // Get the funcref in "rettv".
957 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
958 if (r != OK)
959 {
960 clear_evalarg(&evalarg, NULL);
961 return r;
962 }
963
964 // "rettv" will now be a partial referencing the function.
965 ufunc = rettv.vval.v_partial->pt_func;
966 ++ufunc->uf_refcount;
967 clear_tv(&rettv);
968
969 // Compile it here to get the return type. The return type is optional,
970 // when it's missing use t_unknown. This is recognized in
971 // compile_return().
972 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
973 ufunc->uf_ret_type = &t_unknown;
974 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
975
976 // When the outer function is compiled for profiling or debugging, the
977 // lambda may be called without profiling or debugging. Compile it here in
978 // the right context.
979 if (cctx->ctx_compile_type == CT_DEBUG
980#ifdef FEAT_PROFILE
981 || cctx->ctx_compile_type == CT_PROFILE
982#endif
983 )
984 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
985
986 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
987 // "*arg" may point into it. Point into the original line to avoid a
988 // dangling pointer.
989 if (evalarg.eval_using_cmdline)
990 {
991 garray_T *gap = &evalarg.eval_tofree_ga;
992 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
993
994 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
995 + off;
996 }
997
998 clear_evalarg(&evalarg, NULL);
999
1000 if (ufunc->uf_def_status == UF_COMPILED)
1001 {
1002 // The return type will now be known.
1003 set_function_type(ufunc);
1004
1005 // The function reference count will be 1. When the ISN_FUNCREF
1006 // instruction is deleted the reference count is decremented and the
1007 // function is freed.
1008 return generate_FUNCREF(cctx, ufunc);
1009 }
1010
1011 func_ptr_unref(ufunc);
1012 return FAIL;
1013}
1014
1015/*
1016 * Get a lambda and compile it. Uses Vim9 syntax.
1017 */
1018 int
1019get_lambda_tv_and_compile(
1020 char_u **arg,
1021 typval_T *rettv,
1022 int types_optional,
1023 evalarg_T *evalarg)
1024{
1025 int r;
1026 ufunc_T *ufunc;
1027 int save_sc_version = current_sctx.sc_version;
1028
1029 // Get the funcref in "rettv".
1030 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1031 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1032 current_sctx.sc_version = save_sc_version;
1033 if (r != OK)
1034 return r;
1035
1036 // "rettv" will now be a partial referencing the function.
1037 ufunc = rettv->vval.v_partial->pt_func;
1038
1039 // Compile it here to get the return type. The return type is optional,
1040 // when it's missing use t_unknown. This is recognized in
1041 // compile_return().
1042 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1043 ufunc->uf_ret_type = &t_unknown;
1044 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1045
1046 if (ufunc->uf_def_status == UF_COMPILED)
1047 {
1048 // The return type will now be known.
1049 set_function_type(ufunc);
1050 return OK;
1051 }
1052 clear_tv(rettv);
1053 return FAIL;
1054}
1055
1056/*
1057 * parse a dict: {key: val, [key]: val}
1058 * "*arg" points to the '{'.
1059 * ppconst->pp_is_const is set if all item values are a constant.
1060 */
1061 static int
1062compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1063{
1064 garray_T *instr = &cctx->ctx_instr;
1065 int count = 0;
1066 dict_T *d = dict_alloc();
1067 dictitem_T *item;
1068 char_u *whitep = *arg + 1;
1069 char_u *p;
1070 int is_const;
1071 int is_all_const = TRUE; // reset when non-const encountered
1072
1073 if (d == NULL)
1074 return FAIL;
1075 if (generate_ppconst(cctx, ppconst) == FAIL)
1076 return FAIL;
1077 for (;;)
1078 {
1079 char_u *key = NULL;
1080
1081 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1082 {
1083 *arg = NULL;
1084 goto failret;
1085 }
1086
1087 if (**arg == '}')
1088 break;
1089
1090 if (**arg == '[')
1091 {
1092 isn_T *isn;
1093
1094 // {[expr]: value} uses an evaluated key.
1095 *arg = skipwhite(*arg + 1);
1096 if (compile_expr0(arg, cctx) == FAIL)
1097 return FAIL;
1098 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1099 if (isn->isn_type == ISN_PUSHNR)
1100 {
1101 char buf[NUMBUFLEN];
1102
1103 // Convert to string at compile time.
1104 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1105 isn->isn_type = ISN_PUSHS;
1106 isn->isn_arg.string = vim_strsave((char_u *)buf);
1107 }
1108 if (isn->isn_type == ISN_PUSHS)
1109 key = isn->isn_arg.string;
1110 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1111 return FAIL;
1112 *arg = skipwhite(*arg);
1113 if (**arg != ']')
1114 {
1115 emsg(_(e_missing_matching_bracket_after_dict_key));
1116 return FAIL;
1117 }
1118 ++*arg;
1119 }
1120 else
1121 {
1122 // {"name": value},
1123 // {'name': value},
1124 // {name: value} use "name" as a literal key
1125 key = get_literal_key(arg);
1126 if (key == NULL)
1127 return FAIL;
1128 if (generate_PUSHS(cctx, &key) == FAIL)
1129 return FAIL;
1130 }
1131
1132 // Check for duplicate keys, if using string keys.
1133 if (key != NULL)
1134 {
1135 item = dict_find(d, key, -1);
1136 if (item != NULL)
1137 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001138 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001139 goto failret;
1140 }
1141 item = dictitem_alloc(key);
1142 if (item != NULL)
1143 {
1144 item->di_tv.v_type = VAR_UNKNOWN;
1145 item->di_tv.v_lock = 0;
1146 if (dict_add(d, item) == FAIL)
1147 dictitem_free(item);
1148 }
1149 }
1150
1151 if (**arg != ':')
1152 {
1153 if (*skipwhite(*arg) == ':')
1154 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1155 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001156 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001157 return FAIL;
1158 }
1159 whitep = *arg + 1;
1160 if (!IS_WHITE_OR_NUL(*whitep))
1161 {
1162 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1163 return FAIL;
1164 }
1165
1166 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1167 {
1168 *arg = NULL;
1169 goto failret;
1170 }
1171
1172 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1173 return FAIL;
1174 if (!is_const)
1175 is_all_const = FALSE;
1176 ++count;
1177
1178 whitep = *arg;
1179 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1180 {
1181 *arg = NULL;
1182 goto failret;
1183 }
1184 if (**arg == '}')
1185 break;
1186 if (**arg != ',')
1187 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001188 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001189 goto failret;
1190 }
1191 if (IS_WHITE_OR_NUL(*whitep))
1192 {
1193 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1194 return FAIL;
1195 }
1196 whitep = *arg + 1;
1197 if (!IS_WHITE_OR_NUL(*whitep))
1198 {
1199 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1200 return FAIL;
1201 }
1202 *arg = skipwhite(whitep);
1203 }
1204
1205 *arg = *arg + 1;
1206
1207 // Allow for following comment, after at least one space.
1208 p = skipwhite(*arg);
1209 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1210 *arg += STRLEN(*arg);
1211
1212 dict_unref(d);
1213 ppconst->pp_is_const = is_all_const;
1214 return generate_NEWDICT(cctx, count);
1215
1216failret:
1217 if (*arg == NULL)
1218 {
1219 semsg(_(e_missing_dict_end), _("[end of lines]"));
1220 *arg = (char_u *)"";
1221 }
1222 dict_unref(d);
1223 return FAIL;
1224}
1225
1226/*
1227 * Compile "&option".
1228 */
1229 static int
1230compile_get_option(char_u **arg, cctx_T *cctx)
1231{
1232 typval_T rettv;
1233 char_u *start = *arg;
1234 int ret;
1235
1236 // parse the option and get the current value to get the type.
1237 rettv.v_type = VAR_UNKNOWN;
1238 ret = eval_option(arg, &rettv, TRUE);
1239 if (ret == OK)
1240 {
1241 // include the '&' in the name, eval_option() expects it.
1242 char_u *name = vim_strnsave(start, *arg - start);
1243 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1244 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1245
1246 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1247 vim_free(name);
1248 }
1249 clear_tv(&rettv);
1250
1251 return ret;
1252}
1253
1254/*
1255 * Compile "$VAR".
1256 */
1257 static int
1258compile_get_env(char_u **arg, cctx_T *cctx)
1259{
1260 char_u *start = *arg;
1261 int len;
1262 int ret;
1263 char_u *name;
1264
1265 ++*arg;
1266 len = get_env_len(arg);
1267 if (len == 0)
1268 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001269 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001270 return FAIL;
1271 }
1272
1273 // include the '$' in the name, eval_env_var() expects it.
1274 name = vim_strnsave(start, len + 1);
1275 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1276 vim_free(name);
1277 return ret;
1278}
1279
1280/*
1281 * Compile "@r".
1282 */
1283 static int
1284compile_get_register(char_u **arg, cctx_T *cctx)
1285{
1286 int ret;
1287
1288 ++*arg;
1289 if (**arg == NUL)
1290 {
1291 semsg(_(e_syntax_error_at_str), *arg - 1);
1292 return FAIL;
1293 }
1294 if (!valid_yank_reg(**arg, FALSE))
1295 {
1296 emsg_invreg(**arg);
1297 return FAIL;
1298 }
1299 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1300 ++*arg;
1301 return ret;
1302}
1303
1304/*
1305 * Apply leading '!', '-' and '+' to constant "rettv".
1306 * When "numeric_only" is TRUE do not apply '!'.
1307 */
1308 static int
1309apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1310{
1311 char_u *p = *end;
1312
1313 // this works from end to start
1314 while (p > start)
1315 {
1316 --p;
1317 if (*p == '-' || *p == '+')
1318 {
1319 // only '-' has an effect, for '+' we only check the type
1320#ifdef FEAT_FLOAT
1321 if (rettv->v_type == VAR_FLOAT)
1322 {
1323 if (*p == '-')
1324 rettv->vval.v_float = -rettv->vval.v_float;
1325 }
1326 else
1327#endif
1328 {
1329 varnumber_T val;
1330 int error = FALSE;
1331
1332 // tv_get_number_chk() accepts a string, but we don't want that
1333 // here
1334 if (check_not_string(rettv) == FAIL)
1335 return FAIL;
1336 val = tv_get_number_chk(rettv, &error);
1337 clear_tv(rettv);
1338 if (error)
1339 return FAIL;
1340 if (*p == '-')
1341 val = -val;
1342 rettv->v_type = VAR_NUMBER;
1343 rettv->vval.v_number = val;
1344 }
1345 }
1346 else if (numeric_only)
1347 {
1348 ++p;
1349 break;
1350 }
1351 else if (*p == '!')
1352 {
1353 int v = tv2bool(rettv);
1354
1355 // '!' is permissive in the type.
1356 clear_tv(rettv);
1357 rettv->v_type = VAR_BOOL;
1358 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1359 }
1360 }
1361 *end = p;
1362 return OK;
1363}
1364
1365/*
1366 * Recognize v: variables that are constants and set "rettv".
1367 */
1368 static void
1369get_vim_constant(char_u **arg, typval_T *rettv)
1370{
1371 if (STRNCMP(*arg, "v:true", 6) == 0)
1372 {
1373 rettv->v_type = VAR_BOOL;
1374 rettv->vval.v_number = VVAL_TRUE;
1375 *arg += 6;
1376 }
1377 else if (STRNCMP(*arg, "v:false", 7) == 0)
1378 {
1379 rettv->v_type = VAR_BOOL;
1380 rettv->vval.v_number = VVAL_FALSE;
1381 *arg += 7;
1382 }
1383 else if (STRNCMP(*arg, "v:null", 6) == 0)
1384 {
1385 rettv->v_type = VAR_SPECIAL;
1386 rettv->vval.v_number = VVAL_NULL;
1387 *arg += 6;
1388 }
1389 else if (STRNCMP(*arg, "v:none", 6) == 0)
1390 {
1391 rettv->v_type = VAR_SPECIAL;
1392 rettv->vval.v_number = VVAL_NONE;
1393 *arg += 6;
1394 }
1395}
1396
1397 exprtype_T
1398get_compare_type(char_u *p, int *len, int *type_is)
1399{
1400 exprtype_T type = EXPR_UNKNOWN;
1401 int i;
1402
1403 switch (p[0])
1404 {
1405 case '=': if (p[1] == '=')
1406 type = EXPR_EQUAL;
1407 else if (p[1] == '~')
1408 type = EXPR_MATCH;
1409 break;
1410 case '!': if (p[1] == '=')
1411 type = EXPR_NEQUAL;
1412 else if (p[1] == '~')
1413 type = EXPR_NOMATCH;
1414 break;
1415 case '>': if (p[1] != '=')
1416 {
1417 type = EXPR_GREATER;
1418 *len = 1;
1419 }
1420 else
1421 type = EXPR_GEQUAL;
1422 break;
1423 case '<': if (p[1] != '=')
1424 {
1425 type = EXPR_SMALLER;
1426 *len = 1;
1427 }
1428 else
1429 type = EXPR_SEQUAL;
1430 break;
1431 case 'i': if (p[1] == 's')
1432 {
1433 // "is" and "isnot"; but not a prefix of a name
1434 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1435 *len = 5;
1436 i = p[*len];
1437 if (!isalnum(i) && i != '_')
1438 {
1439 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1440 *type_is = TRUE;
1441 }
1442 }
1443 break;
1444 }
1445 return type;
1446}
1447
1448/*
1449 * Skip over an expression, ignoring most errors.
1450 */
1451 void
1452skip_expr_cctx(char_u **arg, cctx_T *cctx)
1453{
1454 evalarg_T evalarg;
1455
1456 init_evalarg(&evalarg);
1457 evalarg.eval_cctx = cctx;
1458 skip_expr(arg, &evalarg);
1459 clear_evalarg(&evalarg, NULL);
1460}
1461
1462/*
1463 * Check that the top of the type stack has a type that can be used as a
1464 * condition. Give an error and return FAIL if not.
1465 */
1466 int
1467bool_on_stack(cctx_T *cctx)
1468{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001469 type_T *type;
1470
Bram Moolenaar078a4612022-01-04 15:17:03 +00001471 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001472 if (type == &t_bool)
1473 return OK;
1474
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001475 if (type == &t_any
1476 || type == &t_unknown
1477 || type == &t_number
1478 || type == &t_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001479 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1480 // This requires a runtime type check.
1481 return generate_COND2BOOL(cctx);
1482
1483 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1484}
1485
1486/*
1487 * Give the "white on both sides" error, taking the operator from "p[len]".
1488 */
1489 void
1490error_white_both(char_u *op, int len)
1491{
1492 char_u buf[10];
1493
1494 vim_strncpy(buf, op, len);
1495 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1496}
1497
1498/*
1499 * Compile code to apply '-', '+' and '!'.
1500 * When "numeric_only" is TRUE do not apply '!'.
1501 */
1502 static int
1503compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1504{
1505 char_u *p = *end;
1506
1507 // this works from end to start
1508 while (p > start)
1509 {
1510 --p;
1511 while (VIM_ISWHITE(*p))
1512 --p;
1513 if (*p == '-' || *p == '+')
1514 {
1515 int negate = *p == '-';
1516 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001517 type_T *type;
1518
Bram Moolenaar078a4612022-01-04 15:17:03 +00001519 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001520 if (type != &t_float && need_type(type, &t_number,
1521 -1, 0, cctx, FALSE, FALSE) == FAIL)
1522 return FAIL;
1523
1524 while (p > start && (p[-1] == '-' || p[-1] == '+'))
1525 {
1526 --p;
1527 if (*p == '-')
1528 negate = !negate;
1529 }
1530 // only '-' has an effect, for '+' we only check the type
1531 if (negate)
1532 {
1533 isn = generate_instr(cctx, ISN_NEGATENR);
1534 if (isn == NULL)
1535 return FAIL;
1536 }
1537 }
1538 else if (numeric_only)
1539 {
1540 ++p;
1541 break;
1542 }
1543 else
1544 {
1545 int invert = *p == '!';
1546
1547 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1548 {
1549 if (p[-1] == '!')
1550 invert = !invert;
1551 --p;
1552 }
1553 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1554 return FAIL;
1555 }
1556 }
1557 *end = p;
1558 return OK;
1559}
1560
1561/*
1562 * Compile "(expression)": recursive!
1563 * Return FAIL/OK.
1564 */
1565 static int
1566compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1567{
1568 int ret;
1569 char_u *p = *arg + 1;
1570
1571 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1572 return FAIL;
1573 if (ppconst->pp_used <= PPSIZE - 10)
1574 {
1575 ret = compile_expr1(arg, cctx, ppconst);
1576 }
1577 else
1578 {
1579 // Not enough space in ppconst, flush constants.
1580 if (generate_ppconst(cctx, ppconst) == FAIL)
1581 return FAIL;
1582 ret = compile_expr0(arg, cctx);
1583 }
1584 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1585 return FAIL;
1586 if (**arg == ')')
1587 ++*arg;
1588 else if (ret == OK)
1589 {
1590 emsg(_(e_missing_closing_paren));
1591 ret = FAIL;
1592 }
1593 return ret;
1594}
1595
Bram Moolenaarc7349932022-01-16 20:59:39 +00001596static int compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1597
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001598/*
1599 * Compile whatever comes after "name" or "name()".
1600 * Advances "*arg" only when something was recognized.
1601 */
1602 static int
1603compile_subscript(
1604 char_u **arg,
1605 cctx_T *cctx,
1606 char_u *start_leader,
1607 char_u **end_leader,
1608 ppconst_T *ppconst)
1609{
1610 char_u *name_start = *end_leader;
1611 int keeping_dict = FALSE;
1612
1613 for (;;)
1614 {
1615 char_u *p = skipwhite(*arg);
1616
1617 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1618 {
1619 char_u *next = peek_next_line_from_context(cctx);
1620
1621 // If a following line starts with "->{" or "->X" advance to that
1622 // line, so that a line break before "->" is allowed.
1623 // Also if a following line starts with ".x".
1624 if (next != NULL &&
1625 ((next[0] == '-' && next[1] == '>'
1626 && (next[2] == '{'
1627 || ASCII_ISALPHA(*skipwhite(next + 2))))
1628 || (next[0] == '.' && eval_isdictc(next[1]))))
1629 {
1630 next = next_line_from_context(cctx, TRUE);
1631 if (next == NULL)
1632 return FAIL;
1633 *arg = next;
1634 p = skipwhite(*arg);
1635 }
1636 }
1637
1638 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1639 // is not a function call.
1640 if (**arg == '(')
1641 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001642 type_T *type;
1643 int argcount = 0;
1644
1645 if (generate_ppconst(cctx, ppconst) == FAIL)
1646 return FAIL;
1647 ppconst->pp_is_const = FALSE;
1648
1649 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001650 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001651
1652 *arg = skipwhite(p + 1);
1653 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
1654 return FAIL;
1655 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1656 return FAIL;
1657 if (keeping_dict)
1658 {
1659 keeping_dict = FALSE;
1660 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1661 return FAIL;
1662 }
1663 }
1664 else if (*p == '-' && p[1] == '>')
1665 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001666 char_u *pstart = p;
1667 int alt;
1668 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001669
Bram Moolenaarc7349932022-01-16 20:59:39 +00001670 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001671 if (generate_ppconst(cctx, ppconst) == FAIL)
1672 return FAIL;
1673 ppconst->pp_is_const = FALSE;
1674
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001675 // Apply the '!', '-' and '+' first:
1676 // -1.0->func() works like (-1.0)->func()
1677 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1678 return FAIL;
1679
1680 p += 2;
1681 *arg = skipwhite(p);
1682 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001683
1684 // Three alternatives handled here:
1685 // 1. "base->name(" only a name, use compile_call()
1686 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1687 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001688 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001689 alt = 2;
1690 else
1691 {
1692 // alternative 1 or 3
1693 p = *arg;
1694 if (!eval_isnamec1(*p))
1695 {
1696 semsg(_(e_trailing_characters_str), pstart);
1697 return FAIL;
1698 }
1699 if (ASCII_ISALPHA(*p) && p[1] == ':')
1700 p += 2;
1701 for ( ; eval_isnamec(*p); ++p)
1702 ;
1703 if (*p == '(')
1704 {
1705 // alternative 1
1706 alt = 1;
1707 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1708 return FAIL;
1709 }
1710 else
1711 {
1712 // Must be alternative 3, find the "(". Only works within
1713 // one line.
1714 alt = 3;
1715 paren = vim_strchr(p, '(');
1716 if (paren == NULL)
1717 {
1718 semsg(_(e_missing_parenthesis_str), *arg);
1719 return FAIL;
1720 }
1721 }
1722 }
1723
1724 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001725 {
1726 int argcount = 1;
1727 garray_T *stack = &cctx->ctx_type_stack;
1728 int type_idx_start = stack->ga_len;
1729 type_T *type;
1730 int expr_isn_start = cctx->ctx_instr.ga_len;
1731 int expr_isn_end;
1732 int arg_isn_count;
1733
Bram Moolenaarc7349932022-01-16 20:59:39 +00001734 if (alt == 2)
1735 {
1736 // Funcref call: list->(Refs[2])(arg)
1737 // or lambda: list->((arg) => expr)(arg)
1738 //
1739 // Fist compile the function expression.
1740 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1741 return FAIL;
1742 }
1743 else
1744 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001745 int fail;
1746 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
1747
Bram Moolenaarc7349932022-01-16 20:59:39 +00001748 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001749
1750 // instead of using LOADG for "import.Func" use PUSHFUNC
1751 ++paren_follows_after_expr;
1752
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001753 // do not look in the next line
1754 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001755
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001756 fail = compile_expr8(arg, cctx, ppconst) == FAIL
1757 || *skipwhite(*arg) != NUL;
1758 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001759 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001760 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001761
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001762 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001763 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001764 semsg(_(e_invalid_expression_str), pstart);
1765 return FAIL;
1766 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001767 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001768
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001769 // Compile the arguments.
1770 if (**arg != '(')
1771 {
1772 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001773 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001774 else
1775 semsg(_(e_missing_parenthesis_str), *arg);
1776 return FAIL;
1777 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001778
1779 // Remember the next instruction index, where the instructions
1780 // for arguments are being written.
1781 expr_isn_end = cctx->ctx_instr.ga_len;
1782
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001783 *arg = skipwhite(*arg + 1);
1784 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
1785 return FAIL;
1786
1787 // Move the instructions for the arguments to before the
1788 // instructions of the expression and move the type of the
1789 // expression after the argument types. This is what ISN_PCALL
1790 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001791 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1792 if (arg_isn_count > 0)
1793 {
1794 int expr_isn_count = expr_isn_end - expr_isn_start;
1795 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001796 type_T *decl_type;
1797 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001798
1799 if (isn == NULL)
1800 return FAIL;
1801 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1802 + expr_isn_start,
1803 sizeof(isn_T) * expr_isn_count);
1804 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1805 + expr_isn_start,
1806 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1807 sizeof(isn_T) * arg_isn_count);
1808 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1809 + expr_isn_start + arg_isn_count,
1810 isn, sizeof(isn_T) * expr_isn_count);
1811 vim_free(isn);
1812
Bram Moolenaar078a4612022-01-04 15:17:03 +00001813 typep = ((type2_T *)stack->ga_data) + type_idx_start;
1814 type = typep->type_curr;
1815 decl_type = typep->type_decl;
1816 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
1817 ((type2_T *)stack->ga_data) + type_idx_start + 1,
1818 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001819 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00001820 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
1821 typep->type_curr = type;
1822 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001823 }
1824
Bram Moolenaar078a4612022-01-04 15:17:03 +00001825 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001826 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
1827 return FAIL;
1828 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001829
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001830 if (keeping_dict)
1831 {
1832 keeping_dict = FALSE;
1833 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1834 return FAIL;
1835 }
1836 }
1837 else if (**arg == '[')
1838 {
1839 int is_slice = FALSE;
1840
1841 // list index: list[123]
1842 // dict member: dict[key]
1843 // string index: text[123]
1844 // blob index: blob[123]
1845 if (generate_ppconst(cctx, ppconst) == FAIL)
1846 return FAIL;
1847 ppconst->pp_is_const = FALSE;
1848
1849 ++p;
1850 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1851 return FAIL;
1852 if (**arg == ':')
1853 {
1854 // missing first index is equal to zero
1855 generate_PUSHNR(cctx, 0);
1856 }
1857 else
1858 {
1859 if (compile_expr0(arg, cctx) == FAIL)
1860 return FAIL;
1861 if (**arg == ':')
1862 {
1863 semsg(_(e_white_space_required_before_and_after_str_at_str),
1864 ":", *arg);
1865 return FAIL;
1866 }
1867 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1868 return FAIL;
1869 *arg = skipwhite(*arg);
1870 }
1871 if (**arg == ':')
1872 {
1873 is_slice = TRUE;
1874 ++*arg;
1875 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
1876 {
1877 semsg(_(e_white_space_required_before_and_after_str_at_str),
1878 ":", *arg);
1879 return FAIL;
1880 }
1881 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1882 return FAIL;
1883 if (**arg == ']')
1884 // missing second index is equal to end of string
1885 generate_PUSHNR(cctx, -1);
1886 else
1887 {
1888 if (compile_expr0(arg, cctx) == FAIL)
1889 return FAIL;
1890 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1891 return FAIL;
1892 *arg = skipwhite(*arg);
1893 }
1894 }
1895
1896 if (**arg != ']')
1897 {
1898 emsg(_(e_missing_closing_square_brace));
1899 return FAIL;
1900 }
1901 *arg = *arg + 1;
1902
1903 if (keeping_dict)
1904 {
1905 keeping_dict = FALSE;
1906 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1907 return FAIL;
1908 }
1909 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
1910 return FAIL;
1911 }
1912 else if (*p == '.' && p[1] != '.')
1913 {
1914 // dictionary member: dict.name
1915 if (generate_ppconst(cctx, ppconst) == FAIL)
1916 return FAIL;
1917 ppconst->pp_is_const = FALSE;
1918
1919 *arg = p + 1;
1920 if (IS_WHITE_OR_NUL(**arg))
1921 {
1922 emsg(_(e_missing_name_after_dot));
1923 return FAIL;
1924 }
1925 p = *arg;
1926 if (eval_isdictc(*p))
1927 while (eval_isnamec(*p))
1928 MB_PTR_ADV(p);
1929 if (p == *arg)
1930 {
1931 semsg(_(e_syntax_error_at_str), *arg);
1932 return FAIL;
1933 }
1934 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
1935 return FAIL;
1936 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
1937 return FAIL;
1938 keeping_dict = TRUE;
1939 *arg = p;
1940 }
1941 else
1942 break;
1943 }
1944
1945 // Turn "dict.Func" into a partial for "Func" bound to "dict".
1946 // This needs to be done at runtime to be able to check the type.
1947 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
1948 return FAIL;
1949
1950 return OK;
1951}
1952
1953/*
1954 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
1955 * "arg" is advanced until after the expression, skipping white space.
1956 *
1957 * If the value is a constant "ppconst->pp_used" will be non-zero.
1958 * Before instructions are generated, any values in "ppconst" will generated.
1959 *
1960 * This is the compiling equivalent of eval1(), eval2(), etc.
1961 */
1962
1963/*
1964 * number number constant
1965 * 0zFFFFFFFF Blob constant
1966 * "string" string constant
1967 * 'string' literal string constant
1968 * &option-name option value
1969 * @r register contents
1970 * identifier variable value
1971 * function() function call
1972 * $VAR environment variable
1973 * (expression) nested expression
1974 * [expr, expr] List
1975 * {key: val, [key]: val} Dictionary
1976 *
1977 * Also handle:
1978 * ! in front logical NOT
1979 * - in front unary minus
1980 * + in front unary plus (ignored)
1981 * trailing (arg) funcref/partial call
1982 * trailing [] subscript in String or List
1983 * trailing .name entry in Dictionary
1984 * trailing ->name() method call
1985 */
1986 static int
Bram Moolenaar5da36052021-12-27 15:39:57 +00001987compile_expr8(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001988 char_u **arg,
1989 cctx_T *cctx,
1990 ppconst_T *ppconst)
1991{
1992 char_u *start_leader, *end_leader;
1993 int ret = OK;
1994 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
1995 int used_before = ppconst->pp_used;
1996
1997 ppconst->pp_is_const = FALSE;
1998
1999 /*
2000 * Skip '!', '-' and '+' characters. They are handled later.
2001 */
2002 start_leader = *arg;
2003 if (eval_leader(arg, TRUE) == FAIL)
2004 return FAIL;
2005 end_leader = *arg;
2006
2007 rettv->v_type = VAR_UNKNOWN;
2008 switch (**arg)
2009 {
2010 /*
2011 * Number constant.
2012 */
2013 case '0': // also for blob starting with 0z
2014 case '1':
2015 case '2':
2016 case '3':
2017 case '4':
2018 case '5':
2019 case '6':
2020 case '7':
2021 case '8':
2022 case '9':
2023 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2024 return FAIL;
2025 // Apply "-" and "+" just before the number now, right to
2026 // left. Matters especially when "->" follows. Stops at
2027 // '!'.
2028 if (apply_leader(rettv, TRUE,
2029 start_leader, &end_leader) == FAIL)
2030 {
2031 clear_tv(rettv);
2032 return FAIL;
2033 }
2034 break;
2035
2036 /*
2037 * String constant: "string".
2038 */
2039 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
2040 return FAIL;
2041 break;
2042
2043 /*
2044 * Literal string constant: 'str''ing'.
2045 */
2046 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
2047 return FAIL;
2048 break;
2049
2050 /*
2051 * Constant Vim variable.
2052 */
2053 case 'v': get_vim_constant(arg, rettv);
2054 ret = NOTDONE;
2055 break;
2056
2057 /*
2058 * "true" constant
2059 */
2060 case 't': if (STRNCMP(*arg, "true", 4) == 0
2061 && !eval_isnamec((*arg)[4]))
2062 {
2063 *arg += 4;
2064 rettv->v_type = VAR_BOOL;
2065 rettv->vval.v_number = VVAL_TRUE;
2066 }
2067 else
2068 ret = NOTDONE;
2069 break;
2070
2071 /*
2072 * "false" constant
2073 */
2074 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2075 && !eval_isnamec((*arg)[5]))
2076 {
2077 *arg += 5;
2078 rettv->v_type = VAR_BOOL;
2079 rettv->vval.v_number = VVAL_FALSE;
2080 }
2081 else
2082 ret = NOTDONE;
2083 break;
2084
2085 /*
2086 * "null" constant
2087 */
2088 case 'n': if (STRNCMP(*arg, "null", 4) == 0
2089 && !eval_isnamec((*arg)[4]))
2090 {
2091 *arg += 4;
2092 rettv->v_type = VAR_SPECIAL;
2093 rettv->vval.v_number = VVAL_NULL;
2094 }
2095 else
2096 ret = NOTDONE;
2097 break;
2098
2099 /*
2100 * List: [expr, expr]
2101 */
2102 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2103 return FAIL;
2104 ret = compile_list(arg, cctx, ppconst);
2105 break;
2106
2107 /*
2108 * Dictionary: {'key': val, 'key': val}
2109 */
2110 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2111 return FAIL;
2112 ret = compile_dict(arg, cctx, ppconst);
2113 break;
2114
2115 /*
2116 * Option value: &name
2117 */
2118 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2119 return FAIL;
2120 ret = compile_get_option(arg, cctx);
2121 break;
2122
2123 /*
2124 * Environment variable: $VAR.
2125 */
2126 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2127 return FAIL;
2128 ret = compile_get_env(arg, cctx);
2129 break;
2130
2131 /*
2132 * Register contents: @r.
2133 */
2134 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2135 return FAIL;
2136 ret = compile_get_register(arg, cctx);
2137 break;
2138 /*
2139 * nested expression: (expression).
2140 * lambda: (arg, arg) => expr
2141 * funcref: (arg, arg) => { statement }
2142 */
2143 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2144 ret = compile_lambda(arg, cctx);
2145 if (ret == NOTDONE)
2146 ret = compile_parenthesis(arg, cctx, ppconst);
2147 break;
2148
2149 default: ret = NOTDONE;
2150 break;
2151 }
2152 if (ret == FAIL)
2153 return FAIL;
2154
2155 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2156 {
2157 if (cctx->ctx_skip == SKIP_YES)
2158 clear_tv(rettv);
2159 else
2160 // A constant expression can possibly be handled compile time,
2161 // return the value instead of generating code.
2162 ++ppconst->pp_used;
2163 }
2164 else if (ret == NOTDONE)
2165 {
2166 char_u *p;
2167 int r;
2168
2169 if (!eval_isnamec1(**arg))
2170 {
2171 if (!vim9_bad_comment(*arg))
2172 {
2173 if (ends_excmd(*skipwhite(*arg)))
2174 semsg(_(e_empty_expression_str), *arg);
2175 else
2176 semsg(_(e_name_expected_str), *arg);
2177 }
2178 return FAIL;
2179 }
2180
2181 // "name" or "name()"
2182 p = to_name_end(*arg, TRUE);
2183 if (p - *arg == (size_t)1 && **arg == '_')
2184 {
2185 emsg(_(e_cannot_use_underscore_here));
2186 return FAIL;
2187 }
2188
2189 if (*p == '(')
2190 {
2191 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2192 }
2193 else
2194 {
2195 if (cctx->ctx_skip != SKIP_YES
2196 && generate_ppconst(cctx, ppconst) == FAIL)
2197 return FAIL;
2198 r = compile_load(arg, p, cctx, TRUE, TRUE);
2199 }
2200 if (r == FAIL)
2201 return FAIL;
2202 }
2203
2204 // Handle following "[]", ".member", etc.
2205 // Then deal with prefixed '-', '+' and '!', if not done already.
2206 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2207 ppconst) == FAIL)
2208 return FAIL;
2209 if (ppconst->pp_used > 0)
2210 {
2211 // apply the '!', '-' and '+' before the constant
2212 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2213 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2214 return FAIL;
2215 return OK;
2216 }
2217 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2218 return FAIL;
2219 return OK;
2220}
2221
2222/*
Bram Moolenaar5da36052021-12-27 15:39:57 +00002223 * <type>expr8: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002224 */
2225 static int
Bram Moolenaar5da36052021-12-27 15:39:57 +00002226compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002227{
2228 type_T *want_type = NULL;
2229
2230 // Recognize <type>
2231 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2232 {
2233 ++*arg;
2234 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2235 if (want_type == NULL)
2236 return FAIL;
2237
2238 if (**arg != '>')
2239 {
2240 if (*skipwhite(*arg) == '>')
2241 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2242 else
2243 emsg(_(e_missing_gt));
2244 return FAIL;
2245 }
2246 ++*arg;
2247 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2248 return FAIL;
2249 }
2250
Bram Moolenaar5da36052021-12-27 15:39:57 +00002251 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002252 return FAIL;
2253
2254 if (want_type != NULL)
2255 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002256 type_T *actual;
2257 where_T where = WHERE_INIT;
2258
2259 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002260 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002261 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002262 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002263 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2264 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002265 return FAIL;
2266 }
2267 }
2268
2269 return OK;
2270}
2271
2272/*
2273 * * number multiplication
2274 * / number division
2275 * % number modulo
2276 */
2277 static int
2278compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2279{
2280 char_u *op;
2281 char_u *next;
2282 int ppconst_used = ppconst->pp_used;
2283
2284 // get the first expression
Bram Moolenaar5da36052021-12-27 15:39:57 +00002285 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002286 return FAIL;
2287
2288 /*
2289 * Repeat computing, until no "*", "/" or "%" is following.
2290 */
2291 for (;;)
2292 {
2293 op = may_peek_next_line(cctx, *arg, &next);
2294 if (*op != '*' && *op != '/' && *op != '%')
2295 break;
2296 if (next != NULL)
2297 {
2298 *arg = next_line_from_context(cctx, TRUE);
2299 op = skipwhite(*arg);
2300 }
2301
2302 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2303 {
2304 error_white_both(op, 1);
2305 return FAIL;
2306 }
2307 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2308 return FAIL;
2309
2310 // get the second expression
Bram Moolenaar5da36052021-12-27 15:39:57 +00002311 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002312 return FAIL;
2313
2314 if (ppconst->pp_used == ppconst_used + 2
2315 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2316 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2317 {
2318 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2319 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2320 varnumber_T res = 0;
2321 int failed = FALSE;
2322
2323 // both are numbers: compute the result
2324 switch (*op)
2325 {
2326 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2327 break;
2328 case '/': res = num_divide(tv1->vval.v_number,
2329 tv2->vval.v_number, &failed);
2330 break;
2331 case '%': res = num_modulus(tv1->vval.v_number,
2332 tv2->vval.v_number, &failed);
2333 break;
2334 }
2335 if (failed)
2336 return FAIL;
2337 tv1->vval.v_number = res;
2338 --ppconst->pp_used;
2339 }
2340 else
2341 {
2342 generate_ppconst(cctx, ppconst);
2343 generate_two_op(cctx, op);
2344 }
2345 }
2346
2347 return OK;
2348}
2349
2350/*
2351 * + number addition or list/blobl concatenation
2352 * - number subtraction
2353 * .. string concatenation
2354 */
2355 static int
2356compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2357{
2358 char_u *op;
2359 char_u *next;
2360 int oplen;
2361 int ppconst_used = ppconst->pp_used;
2362
2363 // get the first variable
2364 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2365 return FAIL;
2366
2367 /*
2368 * Repeat computing, until no "+", "-" or ".." is following.
2369 */
2370 for (;;)
2371 {
2372 op = may_peek_next_line(cctx, *arg, &next);
2373 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2374 break;
2375 if (op[0] == op[1] && *op != '.' && next)
2376 // Finding "++" or "--" on the next line is a separate command.
2377 // But ".." is concatenation.
2378 break;
2379 oplen = (*op == '.' ? 2 : 1);
2380 if (next != NULL)
2381 {
2382 *arg = next_line_from_context(cctx, TRUE);
2383 op = skipwhite(*arg);
2384 }
2385
2386 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2387 {
2388 error_white_both(op, oplen);
2389 return FAIL;
2390 }
2391
2392 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2393 return FAIL;
2394
2395 // get the second expression
2396 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2397 return FAIL;
2398
2399 if (ppconst->pp_used == ppconst_used + 2
2400 && (*op == '.'
2401 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2402 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2403 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2404 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2405 {
2406 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2407 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2408
2409 // concat/subtract/add constant numbers
2410 if (*op == '+')
2411 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2412 else if (*op == '-')
2413 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2414 else
2415 {
2416 // concatenate constant strings
2417 char_u *s1 = tv1->vval.v_string;
2418 char_u *s2 = tv2->vval.v_string;
2419 size_t len1 = STRLEN(s1);
2420
2421 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2422 if (tv1->vval.v_string == NULL)
2423 {
2424 clear_ppconst(ppconst);
2425 return FAIL;
2426 }
2427 mch_memmove(tv1->vval.v_string, s1, len1);
2428 STRCPY(tv1->vval.v_string + len1, s2);
2429 vim_free(s1);
2430 vim_free(s2);
2431 }
2432 --ppconst->pp_used;
2433 }
2434 else
2435 {
2436 generate_ppconst(cctx, ppconst);
2437 ppconst->pp_is_const = FALSE;
2438 if (*op == '.')
2439 {
2440 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2441 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2442 return FAIL;
2443 generate_instr_drop(cctx, ISN_CONCAT, 1);
2444 }
2445 else
2446 generate_two_op(cctx, op);
2447 }
2448 }
2449
2450 return OK;
2451}
2452
2453/*
2454 * expr5a == expr5b
2455 * expr5a =~ expr5b
2456 * expr5a != expr5b
2457 * expr5a !~ expr5b
2458 * expr5a > expr5b
2459 * expr5a >= expr5b
2460 * expr5a < expr5b
2461 * expr5a <= expr5b
2462 * expr5a is expr5b
2463 * expr5a isnot expr5b
2464 *
2465 * Produces instructions:
2466 * EVAL expr5a Push result of "expr5a"
2467 * EVAL expr5b Push result of "expr5b"
2468 * COMPARE one of the compare instructions
2469 */
2470 static int
2471compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2472{
2473 exprtype_T type = EXPR_UNKNOWN;
2474 char_u *p;
2475 char_u *next;
2476 int len = 2;
2477 int type_is = FALSE;
2478 int ppconst_used = ppconst->pp_used;
2479
2480 // get the first variable
2481 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2482 return FAIL;
2483
2484 p = may_peek_next_line(cctx, *arg, &next);
2485 type = get_compare_type(p, &len, &type_is);
2486
2487 /*
2488 * If there is a comparative operator, use it.
2489 */
2490 if (type != EXPR_UNKNOWN)
2491 {
2492 int ic = FALSE; // Default: do not ignore case
2493
2494 if (next != NULL)
2495 {
2496 *arg = next_line_from_context(cctx, TRUE);
2497 p = skipwhite(*arg);
2498 }
2499 if (type_is && (p[len] == '?' || p[len] == '#'))
2500 {
2501 semsg(_(e_invalid_expression_str), *arg);
2502 return FAIL;
2503 }
2504 // extra question mark appended: ignore case
2505 if (p[len] == '?')
2506 {
2507 ic = TRUE;
2508 ++len;
2509 }
2510 // extra '#' appended: match case (ignored)
2511 else if (p[len] == '#')
2512 ++len;
2513 // nothing appended: match case
2514
2515 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2516 {
2517 error_white_both(p, len);
2518 return FAIL;
2519 }
2520
2521 // get the second variable
2522 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2523 return FAIL;
2524
2525 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2526 return FAIL;
2527
2528 if (ppconst->pp_used == ppconst_used + 2)
2529 {
2530 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2531 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2532 int ret;
2533
2534 // Both sides are a constant, compute the result now.
2535 // First check for a valid combination of types, this is more
2536 // strict than typval_compare().
2537 if (check_compare_types(type, tv1, tv2) == FAIL)
2538 ret = FAIL;
2539 else
2540 {
2541 ret = typval_compare(tv1, tv2, type, ic);
2542 tv1->v_type = VAR_BOOL;
2543 tv1->vval.v_number = tv1->vval.v_number
2544 ? VVAL_TRUE : VVAL_FALSE;
2545 clear_tv(tv2);
2546 --ppconst->pp_used;
2547 }
2548 return ret;
2549 }
2550
2551 generate_ppconst(cctx, ppconst);
2552 return generate_COMPARE(cctx, type, ic);
2553 }
2554
2555 return OK;
2556}
2557
2558static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2559
2560/*
2561 * Compile || or &&.
2562 */
2563 static int
2564compile_and_or(
2565 char_u **arg,
2566 cctx_T *cctx,
2567 char *op,
2568 ppconst_T *ppconst,
2569 int ppconst_used UNUSED)
2570{
2571 char_u *next;
2572 char_u *p = may_peek_next_line(cctx, *arg, &next);
2573 int opchar = *op;
2574
2575 if (p[0] == opchar && p[1] == opchar)
2576 {
2577 garray_T *instr = &cctx->ctx_instr;
2578 garray_T end_ga;
2579 int save_skip = cctx->ctx_skip;
2580
2581 /*
2582 * Repeat until there is no following "||" or "&&"
2583 */
2584 ga_init2(&end_ga, sizeof(int), 10);
2585 while (p[0] == opchar && p[1] == opchar)
2586 {
2587 long start_lnum = SOURCING_LNUM;
2588 long save_sourcing_lnum;
2589 int start_ctx_lnum = cctx->ctx_lnum;
2590 int save_lnum;
2591 int const_used;
2592 int status;
2593 jumpwhen_T jump_when = opchar == '|'
2594 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2595
2596 if (next != NULL)
2597 {
2598 *arg = next_line_from_context(cctx, TRUE);
2599 p = skipwhite(*arg);
2600 }
2601
2602 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2603 {
2604 semsg(_(e_white_space_required_before_and_after_str_at_str),
2605 op, p);
2606 ga_clear(&end_ga);
2607 return FAIL;
2608 }
2609
2610 save_sourcing_lnum = SOURCING_LNUM;
2611 SOURCING_LNUM = start_lnum;
2612 save_lnum = cctx->ctx_lnum;
2613 cctx->ctx_lnum = start_ctx_lnum;
2614
2615 status = check_ppconst_bool(ppconst);
2616 if (status != FAIL)
2617 {
2618 // Use the last ppconst if possible.
2619 if (ppconst->pp_used > 0)
2620 {
2621 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2622 int is_true = tv2bool(tv);
2623
2624 if ((is_true && opchar == '|')
2625 || (!is_true && opchar == '&'))
2626 {
2627 // For "false && expr" and "true || expr" the "expr"
2628 // does not need to be evaluated.
2629 cctx->ctx_skip = SKIP_YES;
2630 clear_tv(tv);
2631 tv->v_type = VAR_BOOL;
2632 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2633 }
2634 else
2635 {
2636 // For "true && expr" and "false || expr" only "expr"
2637 // needs to be evaluated.
2638 --ppconst->pp_used;
2639 jump_when = JUMP_NEVER;
2640 }
2641 }
2642 else
2643 {
2644 // Every part must evaluate to a bool.
2645 status = bool_on_stack(cctx);
2646 }
2647 }
2648 if (status != FAIL)
2649 status = ga_grow(&end_ga, 1);
2650 cctx->ctx_lnum = save_lnum;
2651 if (status == FAIL)
2652 {
2653 ga_clear(&end_ga);
2654 return FAIL;
2655 }
2656
2657 if (jump_when != JUMP_NEVER)
2658 {
2659 if (cctx->ctx_skip != SKIP_YES)
2660 {
2661 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2662 ++end_ga.ga_len;
2663 }
2664 generate_JUMP(cctx, jump_when, 0);
2665 }
2666
2667 // eval the next expression
2668 SOURCING_LNUM = save_sourcing_lnum;
2669 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2670 {
2671 ga_clear(&end_ga);
2672 return FAIL;
2673 }
2674
2675 const_used = ppconst->pp_used;
2676 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
2677 : compile_expr4(arg, cctx, ppconst)) == FAIL)
2678 {
2679 ga_clear(&end_ga);
2680 return FAIL;
2681 }
2682
2683 // "0 || 1" results in true, "1 && 0" results in false.
2684 if (ppconst->pp_used == const_used + 1)
2685 {
2686 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2687
2688 if (tv->v_type == VAR_NUMBER
2689 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
2690 {
2691 tv->vval.v_number = tv->vval.v_number == 1
2692 ? VVAL_TRUE : VVAL_FALSE;
2693 tv->v_type = VAR_BOOL;
2694 }
2695 }
2696
2697 p = may_peek_next_line(cctx, *arg, &next);
2698 }
2699
2700 if (check_ppconst_bool(ppconst) == FAIL)
2701 {
2702 ga_clear(&end_ga);
2703 return FAIL;
2704 }
2705
2706 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
2707 // Every part must evaluate to a bool.
2708 if (bool_on_stack(cctx) == FAIL)
2709 {
2710 ga_clear(&end_ga);
2711 return FAIL;
2712 }
2713
2714 if (end_ga.ga_len > 0)
2715 {
2716 // Fill in the end label in all jumps.
2717 generate_ppconst(cctx, ppconst);
2718 while (end_ga.ga_len > 0)
2719 {
2720 isn_T *isn;
2721
2722 --end_ga.ga_len;
2723 isn = ((isn_T *)instr->ga_data)
2724 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
2725 isn->isn_arg.jump.jump_where = instr->ga_len;
2726 }
2727 }
2728 ga_clear(&end_ga);
2729
2730 cctx->ctx_skip = save_skip;
2731 }
2732
2733 return OK;
2734}
2735
2736/*
2737 * expr4a && expr4a && expr4a logical AND
2738 *
2739 * Produces instructions:
2740 * EVAL expr4a Push result of "expr4a"
2741 * COND2BOOL convert to bool if needed
2742 * JUMP_IF_COND_FALSE end
2743 * EVAL expr4b Push result of "expr4b"
2744 * JUMP_IF_COND_FALSE end
2745 * EVAL expr4c Push result of "expr4c"
2746 * end:
2747 */
2748 static int
2749compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2750{
2751 int ppconst_used = ppconst->pp_used;
2752
2753 // get the first variable
2754 if (compile_expr4(arg, cctx, ppconst) == FAIL)
2755 return FAIL;
2756
2757 // || and && work almost the same
2758 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
2759}
2760
2761/*
2762 * expr3a || expr3b || expr3c logical OR
2763 *
2764 * Produces instructions:
2765 * EVAL expr3a Push result of "expr3a"
2766 * COND2BOOL convert to bool if needed
2767 * JUMP_IF_COND_TRUE end
2768 * EVAL expr3b Push result of "expr3b"
2769 * JUMP_IF_COND_TRUE end
2770 * EVAL expr3c Push result of "expr3c"
2771 * end:
2772 */
2773 static int
2774compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2775{
2776 int ppconst_used = ppconst->pp_used;
2777
2778 // eval the first expression
2779 if (compile_expr3(arg, cctx, ppconst) == FAIL)
2780 return FAIL;
2781
2782 // || and && work almost the same
2783 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
2784}
2785
2786/*
2787 * Toplevel expression: expr2 ? expr1a : expr1b
2788 * Produces instructions:
2789 * EVAL expr2 Push result of "expr2"
2790 * JUMP_IF_FALSE alt jump if false
2791 * EVAL expr1a
2792 * JUMP_ALWAYS end
2793 * alt: EVAL expr1b
2794 * end:
2795 *
2796 * Toplevel expression: expr2 ?? expr1
2797 * Produces instructions:
2798 * EVAL expr2 Push result of "expr2"
2799 * JUMP_AND_KEEP_IF_TRUE end jump if true
2800 * EVAL expr1
2801 * end:
2802 */
2803 int
2804compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2805{
2806 char_u *p;
2807 int ppconst_used = ppconst->pp_used;
2808 char_u *next;
2809
2810 // Ignore all kinds of errors when not producing code.
2811 if (cctx->ctx_skip == SKIP_YES)
2812 {
2813 skip_expr_cctx(arg, cctx);
2814 return OK;
2815 }
2816
2817 // Evaluate the first expression.
2818 if (compile_expr2(arg, cctx, ppconst) == FAIL)
2819 return FAIL;
2820
2821 p = may_peek_next_line(cctx, *arg, &next);
2822 if (*p == '?')
2823 {
2824 int op_falsy = p[1] == '?';
2825 garray_T *instr = &cctx->ctx_instr;
2826 garray_T *stack = &cctx->ctx_type_stack;
2827 int alt_idx = instr->ga_len;
2828 int end_idx = 0;
2829 isn_T *isn;
2830 type_T *type1 = NULL;
2831 int has_const_expr = FALSE;
2832 int const_value = FALSE;
2833 int save_skip = cctx->ctx_skip;
2834
2835 if (next != NULL)
2836 {
2837 *arg = next_line_from_context(cctx, TRUE);
2838 p = skipwhite(*arg);
2839 }
2840
2841 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
2842 {
2843 semsg(_(e_white_space_required_before_and_after_str_at_str),
2844 op_falsy ? "??" : "?", p);
2845 return FAIL;
2846 }
2847
2848 if (ppconst->pp_used == ppconst_used + 1)
2849 {
2850 // the condition is a constant, we know whether the ? or the :
2851 // expression is to be evaluated.
2852 has_const_expr = TRUE;
2853 if (op_falsy)
2854 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
2855 else
2856 {
2857 int error = FALSE;
2858
2859 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
2860 &error);
2861 if (error)
2862 return FAIL;
2863 }
2864 cctx->ctx_skip = save_skip == SKIP_YES ||
2865 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
2866
2867 if (op_falsy && cctx->ctx_skip == SKIP_YES)
2868 // "left ?? right" and "left" is truthy: produce "left"
2869 generate_ppconst(cctx, ppconst);
2870 else
2871 {
2872 clear_tv(&ppconst->pp_tv[ppconst_used]);
2873 --ppconst->pp_used;
2874 }
2875 }
2876 else
2877 {
2878 generate_ppconst(cctx, ppconst);
2879 if (op_falsy)
2880 end_idx = instr->ga_len;
2881 generate_JUMP(cctx, op_falsy
2882 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
2883 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002884 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002885 }
2886
2887 // evaluate the second expression; any type is accepted
2888 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
2889 return FAIL;
2890 if (compile_expr1(arg, cctx, ppconst) == FAIL)
2891 return FAIL;
2892
2893 if (!has_const_expr)
2894 {
2895 generate_ppconst(cctx, ppconst);
2896
2897 if (!op_falsy)
2898 {
2899 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00002900 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002901 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002902
2903 end_idx = instr->ga_len;
2904 generate_JUMP(cctx, JUMP_ALWAYS, 0);
2905
2906 // jump here from JUMP_IF_FALSE
2907 isn = ((isn_T *)instr->ga_data) + alt_idx;
2908 isn->isn_arg.jump.jump_where = instr->ga_len;
2909 }
2910 }
2911
2912 if (!op_falsy)
2913 {
2914 // Check for the ":".
2915 p = may_peek_next_line(cctx, *arg, &next);
2916 if (*p != ':')
2917 {
2918 emsg(_(e_missing_colon_after_questionmark));
2919 return FAIL;
2920 }
2921 if (next != NULL)
2922 {
2923 *arg = next_line_from_context(cctx, TRUE);
2924 p = skipwhite(*arg);
2925 }
2926
2927 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
2928 {
2929 semsg(_(e_white_space_required_before_and_after_str_at_str),
2930 ":", p);
2931 return FAIL;
2932 }
2933
2934 // evaluate the third expression
2935 if (has_const_expr)
2936 cctx->ctx_skip = save_skip == SKIP_YES || const_value
2937 ? SKIP_YES : SKIP_NOT;
2938 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
2939 return FAIL;
2940 if (compile_expr1(arg, cctx, ppconst) == FAIL)
2941 return FAIL;
2942 }
2943
2944 if (!has_const_expr)
2945 {
2946 type_T **typep;
2947
2948 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00002949 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002950
2951 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00002952 typep = &((((type2_T *)stack->ga_data)
2953 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002954 common_type(type1, *typep, typep, cctx->ctx_type_list);
2955
2956 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
2957 isn = ((isn_T *)instr->ga_data) + end_idx;
2958 isn->isn_arg.jump.jump_where = instr->ga_len;
2959 }
2960
2961 cctx->ctx_skip = save_skip;
2962 }
2963 return OK;
2964}
2965
2966/*
2967 * Toplevel expression.
2968 * Sets "is_const" (if not NULL) to indicate the value is a constant.
2969 * Returns OK or FAIL.
2970 */
2971 int
2972compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
2973{
2974 ppconst_T ppconst;
2975
2976 CLEAR_FIELD(ppconst);
2977 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
2978 {
2979 clear_ppconst(&ppconst);
2980 return FAIL;
2981 }
2982 if (is_const != NULL)
2983 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
2984 if (generate_ppconst(cctx, &ppconst) == FAIL)
2985 return FAIL;
2986 return OK;
2987}
2988
2989/*
2990 * Toplevel expression.
2991 */
2992 int
2993compile_expr0(char_u **arg, cctx_T *cctx)
2994{
2995 return compile_expr0_ext(arg, cctx, NULL);
2996}
2997
2998
2999#endif // defined(FEAT_EVAL)