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