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