blob: a56219fb349f8d58b30802035a4b280d5b673344 [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
Bram Moolenaar06b77222022-01-25 15:51:56 +0000310 // name. If a '(' follows it must be a function. Otherwise we
311 // don't know, it can be "script.Func".
Bram Moolenaard02dce22022-01-18 17:43:04 +0000312 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaard041f422022-01-12 19:54:00 +0000313 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any);
314 else
Bram Moolenaar06b77222022-01-25 15:51:56 +0000315 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
Bram Moolenaard041f422022-01-12 19:54:00 +0000316 vim_free(auto_name);
317 done = TRUE;
318 }
319 else
320 {
321 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000322 cctx, TRUE);
Bram Moolenaard041f422022-01-12 19:54:00 +0000323 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000324 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000325 *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;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000671 imported_T *import;
672
673 if (varlen >= sizeof(namebuf))
674 {
675 semsg(_(e_name_too_long_str), name);
676 return FAIL;
677 }
678 vim_strncpy(namebuf, *arg, varlen);
679
680 import = find_imported(name, varlen, FALSE, cctx);
681 if (import != NULL)
682 {
683 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
684 return FAIL;
685 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000686
687 // We can evaluate "has('name')" at compile time.
688 // We always evaluate "exists_compiled()" at compile time.
689 if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
690 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
691 {
692 char_u *s = skipwhite(*arg + varlen + 1);
693 typval_T argvars[2];
694 int is_has = **arg == 'h';
695
696 argvars[0].v_type = VAR_UNKNOWN;
697 if (*s == '"')
698 (void)eval_string(&s, &argvars[0], TRUE);
699 else if (*s == '\'')
700 (void)eval_lit_string(&s, &argvars[0], TRUE);
701 s = skipwhite(s);
702 if (*s == ')' && argvars[0].v_type == VAR_STRING
703 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
704 || !is_has))
705 {
706 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
707
708 *arg = s + 1;
709 argvars[1].v_type = VAR_UNKNOWN;
710 tv->v_type = VAR_NUMBER;
711 tv->vval.v_number = 0;
712 if (is_has)
713 f_has(argvars, tv);
714 else
715 f_exists(argvars, tv);
716 clear_tv(&argvars[0]);
717 ++ppconst->pp_used;
718 return OK;
719 }
720 clear_tv(&argvars[0]);
721 if (!is_has)
722 {
723 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
724 return FAIL;
725 }
726 }
727
728 if (generate_ppconst(cctx, ppconst) == FAIL)
729 return FAIL;
730
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000731 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
732
733 // We handle the "skip" argument of searchpair() and searchpairpos()
734 // differently.
735 is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
736 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
737 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
738 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
739
740 *arg = skipwhite(*arg + varlen + 1);
741 if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
742 goto theend;
743
744 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
745 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
746 {
747 int idx;
748
749 // builtin function
750 idx = find_internal_func(name);
751 if (idx >= 0)
752 {
753 if (STRCMP(name, "flatten") == 0)
754 {
755 emsg(_(e_cannot_use_flatten_in_vim9_script));
756 goto theend;
757 }
758
759 if (STRCMP(name, "add") == 0 && argcount == 2)
760 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000761 type_T *type = get_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000762
763 // add() can be compiled to instructions if we know the type
764 if (type->tt_type == VAR_LIST)
765 {
766 // inline "add(list, item)" so that the type can be checked
767 res = generate_LISTAPPEND(cctx);
768 idx = -1;
769 }
770 else if (type->tt_type == VAR_BLOB)
771 {
772 // inline "add(blob, nr)" so that the type can be checked
773 res = generate_BLOBAPPEND(cctx);
774 idx = -1;
775 }
776 }
777
778 if (idx >= 0)
779 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
780 }
781 else
782 semsg(_(e_unknown_function_str), namebuf);
783 goto theend;
784 }
785
786 // An argument or local variable can be a function reference, this
787 // overrules a function name.
788 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
789 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
790 {
791 // If we can find the function by name generate the right call.
792 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000793 ufunc = find_func(name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000794 if (ufunc != NULL && !func_is_global(ufunc))
795 {
796 res = generate_CALL(cctx, ufunc, argcount);
797 goto theend;
798 }
799 }
800
801 // If the name is a variable, load it and use PCALL.
802 // Not for g:Func(), we don't know if it is a variable or not.
803 // Not for eome#Func(), it will be loaded later.
804 p = namebuf;
805 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
806 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
807 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000808 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000809
810 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
811 goto theend;
812 }
813
814 // If we can find a global function by name generate the right call.
815 if (ufunc != NULL)
816 {
817 res = generate_CALL(cctx, ufunc, argcount);
818 goto theend;
819 }
820
821 // A global function may be defined only later. Need to figure out at
822 // runtime. Also handles a FuncRef at runtime.
823 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
824 res = generate_UCALL(cctx, name, argcount);
825 else
826 semsg(_(e_unknown_function_str), namebuf);
827
828theend:
829 vim_free(tofree);
830 return res;
831}
832
833// like NAMESPACE_CHAR but with 'a' and 'l'.
834#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
835
836/*
837 * Find the end of a variable or function name. Unlike find_name_end() this
838 * does not recognize magic braces.
839 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
840 * Return a pointer to just after the name. Equal to "arg" if there is no
841 * valid name.
842 */
843 char_u *
844to_name_end(char_u *arg, int use_namespace)
845{
846 char_u *p;
847
848 // Quick check for valid starting character.
849 if (!eval_isnamec1(*arg))
850 return arg;
851
852 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
853 // Include a namespace such as "s:var" and "v:var". But "n:" is not
854 // and can be used in slice "[n:]".
855 if (*p == ':' && (p != arg + 1
856 || !use_namespace
857 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
858 break;
859 return p;
860}
861
862/*
863 * Like to_name_end() but also skip over a list or dict constant.
864 * Also accept "<SNR>123_Func".
865 * This intentionally does not handle line continuation.
866 */
867 char_u *
868to_name_const_end(char_u *arg)
869{
870 char_u *p = arg;
871 typval_T rettv;
872
873 if (STRNCMP(p, "<SNR>", 5) == 0)
874 p = skipdigits(p + 5);
875 p = to_name_end(p, TRUE);
876 if (p == arg && *arg == '[')
877 {
878
879 // Can be "[1, 2, 3]->Func()".
880 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
881 p = arg;
882 }
883 return p;
884}
885
886/*
887 * parse a list: [expr, expr]
888 * "*arg" points to the '['.
889 * ppconst->pp_is_const is set if all items are a constant.
890 */
891 static int
892compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
893{
894 char_u *p = skipwhite(*arg + 1);
895 char_u *whitep = *arg + 1;
896 int count = 0;
897 int is_const;
898 int is_all_const = TRUE; // reset when non-const encountered
899
900 for (;;)
901 {
902 if (may_get_next_line(whitep, &p, cctx) == FAIL)
903 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000904 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000905 return FAIL;
906 }
907 if (*p == ',')
908 {
909 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
910 return FAIL;
911 }
912 if (*p == ']')
913 {
914 ++p;
915 break;
916 }
917 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
918 return FAIL;
919 if (!is_const)
920 is_all_const = FALSE;
921 ++count;
922 if (*p == ',')
923 {
924 ++p;
925 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
926 {
927 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
928 return FAIL;
929 }
930 }
931 whitep = p;
932 p = skipwhite(p);
933 }
934 *arg = p;
935
936 ppconst->pp_is_const = is_all_const;
937 return generate_NEWLIST(cctx, count);
938}
939
940/*
941 * Parse a lambda: "(arg, arg) => expr"
942 * "*arg" points to the '('.
943 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
944 */
945 static int
946compile_lambda(char_u **arg, cctx_T *cctx)
947{
948 int r;
949 typval_T rettv;
950 ufunc_T *ufunc;
951 evalarg_T evalarg;
952
953 init_evalarg(&evalarg);
954 evalarg.eval_flags = EVAL_EVALUATE;
955 evalarg.eval_cctx = cctx;
956
957 // Get the funcref in "rettv".
958 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
959 if (r != OK)
960 {
961 clear_evalarg(&evalarg, NULL);
962 return r;
963 }
964
965 // "rettv" will now be a partial referencing the function.
966 ufunc = rettv.vval.v_partial->pt_func;
967 ++ufunc->uf_refcount;
968 clear_tv(&rettv);
969
970 // Compile it here to get the return type. The return type is optional,
971 // when it's missing use t_unknown. This is recognized in
972 // compile_return().
973 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
974 ufunc->uf_ret_type = &t_unknown;
975 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
976
977 // When the outer function is compiled for profiling or debugging, the
978 // lambda may be called without profiling or debugging. Compile it here in
979 // the right context.
980 if (cctx->ctx_compile_type == CT_DEBUG
981#ifdef FEAT_PROFILE
982 || cctx->ctx_compile_type == CT_PROFILE
983#endif
984 )
985 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
986
987 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
988 // "*arg" may point into it. Point into the original line to avoid a
989 // dangling pointer.
990 if (evalarg.eval_using_cmdline)
991 {
992 garray_T *gap = &evalarg.eval_tofree_ga;
993 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
994
995 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
996 + off;
997 }
998
999 clear_evalarg(&evalarg, NULL);
1000
1001 if (ufunc->uf_def_status == UF_COMPILED)
1002 {
1003 // The return type will now be known.
1004 set_function_type(ufunc);
1005
1006 // The function reference count will be 1. When the ISN_FUNCREF
1007 // instruction is deleted the reference count is decremented and the
1008 // function is freed.
1009 return generate_FUNCREF(cctx, ufunc);
1010 }
1011
1012 func_ptr_unref(ufunc);
1013 return FAIL;
1014}
1015
1016/*
1017 * Get a lambda and compile it. Uses Vim9 syntax.
1018 */
1019 int
1020get_lambda_tv_and_compile(
1021 char_u **arg,
1022 typval_T *rettv,
1023 int types_optional,
1024 evalarg_T *evalarg)
1025{
1026 int r;
1027 ufunc_T *ufunc;
1028 int save_sc_version = current_sctx.sc_version;
1029
1030 // Get the funcref in "rettv".
1031 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1032 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1033 current_sctx.sc_version = save_sc_version;
1034 if (r != OK)
1035 return r;
1036
1037 // "rettv" will now be a partial referencing the function.
1038 ufunc = rettv->vval.v_partial->pt_func;
1039
1040 // Compile it here to get the return type. The return type is optional,
1041 // when it's missing use t_unknown. This is recognized in
1042 // compile_return().
1043 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1044 ufunc->uf_ret_type = &t_unknown;
1045 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1046
1047 if (ufunc->uf_def_status == UF_COMPILED)
1048 {
1049 // The return type will now be known.
1050 set_function_type(ufunc);
1051 return OK;
1052 }
1053 clear_tv(rettv);
1054 return FAIL;
1055}
1056
1057/*
1058 * parse a dict: {key: val, [key]: val}
1059 * "*arg" points to the '{'.
1060 * ppconst->pp_is_const is set if all item values are a constant.
1061 */
1062 static int
1063compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1064{
1065 garray_T *instr = &cctx->ctx_instr;
1066 int count = 0;
1067 dict_T *d = dict_alloc();
1068 dictitem_T *item;
1069 char_u *whitep = *arg + 1;
1070 char_u *p;
1071 int is_const;
1072 int is_all_const = TRUE; // reset when non-const encountered
1073
1074 if (d == NULL)
1075 return FAIL;
1076 if (generate_ppconst(cctx, ppconst) == FAIL)
1077 return FAIL;
1078 for (;;)
1079 {
1080 char_u *key = NULL;
1081
1082 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1083 {
1084 *arg = NULL;
1085 goto failret;
1086 }
1087
1088 if (**arg == '}')
1089 break;
1090
1091 if (**arg == '[')
1092 {
1093 isn_T *isn;
1094
1095 // {[expr]: value} uses an evaluated key.
1096 *arg = skipwhite(*arg + 1);
1097 if (compile_expr0(arg, cctx) == FAIL)
1098 return FAIL;
1099 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1100 if (isn->isn_type == ISN_PUSHNR)
1101 {
1102 char buf[NUMBUFLEN];
1103
1104 // Convert to string at compile time.
1105 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1106 isn->isn_type = ISN_PUSHS;
1107 isn->isn_arg.string = vim_strsave((char_u *)buf);
1108 }
1109 if (isn->isn_type == ISN_PUSHS)
1110 key = isn->isn_arg.string;
1111 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1112 return FAIL;
1113 *arg = skipwhite(*arg);
1114 if (**arg != ']')
1115 {
1116 emsg(_(e_missing_matching_bracket_after_dict_key));
1117 return FAIL;
1118 }
1119 ++*arg;
1120 }
1121 else
1122 {
1123 // {"name": value},
1124 // {'name': value},
1125 // {name: value} use "name" as a literal key
1126 key = get_literal_key(arg);
1127 if (key == NULL)
1128 return FAIL;
1129 if (generate_PUSHS(cctx, &key) == FAIL)
1130 return FAIL;
1131 }
1132
1133 // Check for duplicate keys, if using string keys.
1134 if (key != NULL)
1135 {
1136 item = dict_find(d, key, -1);
1137 if (item != NULL)
1138 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001139 semsg(_(e_duplicate_key_in_dicitonary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001140 goto failret;
1141 }
1142 item = dictitem_alloc(key);
1143 if (item != NULL)
1144 {
1145 item->di_tv.v_type = VAR_UNKNOWN;
1146 item->di_tv.v_lock = 0;
1147 if (dict_add(d, item) == FAIL)
1148 dictitem_free(item);
1149 }
1150 }
1151
1152 if (**arg != ':')
1153 {
1154 if (*skipwhite(*arg) == ':')
1155 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1156 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001157 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001158 return FAIL;
1159 }
1160 whitep = *arg + 1;
1161 if (!IS_WHITE_OR_NUL(*whitep))
1162 {
1163 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1164 return FAIL;
1165 }
1166
1167 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1168 {
1169 *arg = NULL;
1170 goto failret;
1171 }
1172
1173 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1174 return FAIL;
1175 if (!is_const)
1176 is_all_const = FALSE;
1177 ++count;
1178
1179 whitep = *arg;
1180 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1181 {
1182 *arg = NULL;
1183 goto failret;
1184 }
1185 if (**arg == '}')
1186 break;
1187 if (**arg != ',')
1188 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001189 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001190 goto failret;
1191 }
1192 if (IS_WHITE_OR_NUL(*whitep))
1193 {
1194 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1195 return FAIL;
1196 }
1197 whitep = *arg + 1;
1198 if (!IS_WHITE_OR_NUL(*whitep))
1199 {
1200 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1201 return FAIL;
1202 }
1203 *arg = skipwhite(whitep);
1204 }
1205
1206 *arg = *arg + 1;
1207
1208 // Allow for following comment, after at least one space.
1209 p = skipwhite(*arg);
1210 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1211 *arg += STRLEN(*arg);
1212
1213 dict_unref(d);
1214 ppconst->pp_is_const = is_all_const;
1215 return generate_NEWDICT(cctx, count);
1216
1217failret:
1218 if (*arg == NULL)
1219 {
1220 semsg(_(e_missing_dict_end), _("[end of lines]"));
1221 *arg = (char_u *)"";
1222 }
1223 dict_unref(d);
1224 return FAIL;
1225}
1226
1227/*
1228 * Compile "&option".
1229 */
1230 static int
1231compile_get_option(char_u **arg, cctx_T *cctx)
1232{
1233 typval_T rettv;
1234 char_u *start = *arg;
1235 int ret;
1236
1237 // parse the option and get the current value to get the type.
1238 rettv.v_type = VAR_UNKNOWN;
1239 ret = eval_option(arg, &rettv, TRUE);
1240 if (ret == OK)
1241 {
1242 // include the '&' in the name, eval_option() expects it.
1243 char_u *name = vim_strnsave(start, *arg - start);
1244 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1245 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1246
1247 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1248 vim_free(name);
1249 }
1250 clear_tv(&rettv);
1251
1252 return ret;
1253}
1254
1255/*
1256 * Compile "$VAR".
1257 */
1258 static int
1259compile_get_env(char_u **arg, cctx_T *cctx)
1260{
1261 char_u *start = *arg;
1262 int len;
1263 int ret;
1264 char_u *name;
1265
1266 ++*arg;
1267 len = get_env_len(arg);
1268 if (len == 0)
1269 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001270 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001271 return FAIL;
1272 }
1273
1274 // include the '$' in the name, eval_env_var() expects it.
1275 name = vim_strnsave(start, len + 1);
1276 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1277 vim_free(name);
1278 return ret;
1279}
1280
1281/*
1282 * Compile "@r".
1283 */
1284 static int
1285compile_get_register(char_u **arg, cctx_T *cctx)
1286{
1287 int ret;
1288
1289 ++*arg;
1290 if (**arg == NUL)
1291 {
1292 semsg(_(e_syntax_error_at_str), *arg - 1);
1293 return FAIL;
1294 }
1295 if (!valid_yank_reg(**arg, FALSE))
1296 {
1297 emsg_invreg(**arg);
1298 return FAIL;
1299 }
1300 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1301 ++*arg;
1302 return ret;
1303}
1304
1305/*
1306 * Apply leading '!', '-' and '+' to constant "rettv".
1307 * When "numeric_only" is TRUE do not apply '!'.
1308 */
1309 static int
1310apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1311{
1312 char_u *p = *end;
1313
1314 // this works from end to start
1315 while (p > start)
1316 {
1317 --p;
1318 if (*p == '-' || *p == '+')
1319 {
1320 // only '-' has an effect, for '+' we only check the type
1321#ifdef FEAT_FLOAT
1322 if (rettv->v_type == VAR_FLOAT)
1323 {
1324 if (*p == '-')
1325 rettv->vval.v_float = -rettv->vval.v_float;
1326 }
1327 else
1328#endif
1329 {
1330 varnumber_T val;
1331 int error = FALSE;
1332
1333 // tv_get_number_chk() accepts a string, but we don't want that
1334 // here
1335 if (check_not_string(rettv) == FAIL)
1336 return FAIL;
1337 val = tv_get_number_chk(rettv, &error);
1338 clear_tv(rettv);
1339 if (error)
1340 return FAIL;
1341 if (*p == '-')
1342 val = -val;
1343 rettv->v_type = VAR_NUMBER;
1344 rettv->vval.v_number = val;
1345 }
1346 }
1347 else if (numeric_only)
1348 {
1349 ++p;
1350 break;
1351 }
1352 else if (*p == '!')
1353 {
1354 int v = tv2bool(rettv);
1355
1356 // '!' is permissive in the type.
1357 clear_tv(rettv);
1358 rettv->v_type = VAR_BOOL;
1359 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1360 }
1361 }
1362 *end = p;
1363 return OK;
1364}
1365
1366/*
1367 * Recognize v: variables that are constants and set "rettv".
1368 */
1369 static void
1370get_vim_constant(char_u **arg, typval_T *rettv)
1371{
1372 if (STRNCMP(*arg, "v:true", 6) == 0)
1373 {
1374 rettv->v_type = VAR_BOOL;
1375 rettv->vval.v_number = VVAL_TRUE;
1376 *arg += 6;
1377 }
1378 else if (STRNCMP(*arg, "v:false", 7) == 0)
1379 {
1380 rettv->v_type = VAR_BOOL;
1381 rettv->vval.v_number = VVAL_FALSE;
1382 *arg += 7;
1383 }
1384 else if (STRNCMP(*arg, "v:null", 6) == 0)
1385 {
1386 rettv->v_type = VAR_SPECIAL;
1387 rettv->vval.v_number = VVAL_NULL;
1388 *arg += 6;
1389 }
1390 else if (STRNCMP(*arg, "v:none", 6) == 0)
1391 {
1392 rettv->v_type = VAR_SPECIAL;
1393 rettv->vval.v_number = VVAL_NONE;
1394 *arg += 6;
1395 }
1396}
1397
1398 exprtype_T
1399get_compare_type(char_u *p, int *len, int *type_is)
1400{
1401 exprtype_T type = EXPR_UNKNOWN;
1402 int i;
1403
1404 switch (p[0])
1405 {
1406 case '=': if (p[1] == '=')
1407 type = EXPR_EQUAL;
1408 else if (p[1] == '~')
1409 type = EXPR_MATCH;
1410 break;
1411 case '!': if (p[1] == '=')
1412 type = EXPR_NEQUAL;
1413 else if (p[1] == '~')
1414 type = EXPR_NOMATCH;
1415 break;
1416 case '>': if (p[1] != '=')
1417 {
1418 type = EXPR_GREATER;
1419 *len = 1;
1420 }
1421 else
1422 type = EXPR_GEQUAL;
1423 break;
1424 case '<': if (p[1] != '=')
1425 {
1426 type = EXPR_SMALLER;
1427 *len = 1;
1428 }
1429 else
1430 type = EXPR_SEQUAL;
1431 break;
1432 case 'i': if (p[1] == 's')
1433 {
1434 // "is" and "isnot"; but not a prefix of a name
1435 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1436 *len = 5;
1437 i = p[*len];
1438 if (!isalnum(i) && i != '_')
1439 {
1440 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1441 *type_is = TRUE;
1442 }
1443 }
1444 break;
1445 }
1446 return type;
1447}
1448
1449/*
1450 * Skip over an expression, ignoring most errors.
1451 */
1452 void
1453skip_expr_cctx(char_u **arg, cctx_T *cctx)
1454{
1455 evalarg_T evalarg;
1456
1457 init_evalarg(&evalarg);
1458 evalarg.eval_cctx = cctx;
1459 skip_expr(arg, &evalarg);
1460 clear_evalarg(&evalarg, NULL);
1461}
1462
1463/*
1464 * Check that the top of the type stack has a type that can be used as a
1465 * condition. Give an error and return FAIL if not.
1466 */
1467 int
1468bool_on_stack(cctx_T *cctx)
1469{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001470 type_T *type;
1471
Bram Moolenaar078a4612022-01-04 15:17:03 +00001472 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001473 if (type == &t_bool)
1474 return OK;
1475
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001476 if (type == &t_any
1477 || type == &t_unknown
1478 || type == &t_number
1479 || type == &t_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001480 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1481 // This requires a runtime type check.
1482 return generate_COND2BOOL(cctx);
1483
1484 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1485}
1486
1487/*
1488 * Give the "white on both sides" error, taking the operator from "p[len]".
1489 */
1490 void
1491error_white_both(char_u *op, int len)
1492{
1493 char_u buf[10];
1494
1495 vim_strncpy(buf, op, len);
1496 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1497}
1498
1499/*
1500 * Compile code to apply '-', '+' and '!'.
1501 * When "numeric_only" is TRUE do not apply '!'.
1502 */
1503 static int
1504compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1505{
1506 char_u *p = *end;
1507
1508 // this works from end to start
1509 while (p > start)
1510 {
1511 --p;
1512 while (VIM_ISWHITE(*p))
1513 --p;
1514 if (*p == '-' || *p == '+')
1515 {
1516 int negate = *p == '-';
1517 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001518 type_T *type;
1519
Bram Moolenaar078a4612022-01-04 15:17:03 +00001520 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001521 if (type != &t_float && need_type(type, &t_number,
1522 -1, 0, cctx, FALSE, FALSE) == FAIL)
1523 return FAIL;
1524
1525 while (p > start && (p[-1] == '-' || p[-1] == '+'))
1526 {
1527 --p;
1528 if (*p == '-')
1529 negate = !negate;
1530 }
1531 // only '-' has an effect, for '+' we only check the type
1532 if (negate)
1533 {
1534 isn = generate_instr(cctx, ISN_NEGATENR);
1535 if (isn == NULL)
1536 return FAIL;
1537 }
1538 }
1539 else if (numeric_only)
1540 {
1541 ++p;
1542 break;
1543 }
1544 else
1545 {
1546 int invert = *p == '!';
1547
1548 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1549 {
1550 if (p[-1] == '!')
1551 invert = !invert;
1552 --p;
1553 }
1554 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1555 return FAIL;
1556 }
1557 }
1558 *end = p;
1559 return OK;
1560}
1561
1562/*
1563 * Compile "(expression)": recursive!
1564 * Return FAIL/OK.
1565 */
1566 static int
1567compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1568{
1569 int ret;
1570 char_u *p = *arg + 1;
1571
1572 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1573 return FAIL;
1574 if (ppconst->pp_used <= PPSIZE - 10)
1575 {
1576 ret = compile_expr1(arg, cctx, ppconst);
1577 }
1578 else
1579 {
1580 // Not enough space in ppconst, flush constants.
1581 if (generate_ppconst(cctx, ppconst) == FAIL)
1582 return FAIL;
1583 ret = compile_expr0(arg, cctx);
1584 }
1585 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1586 return FAIL;
1587 if (**arg == ')')
1588 ++*arg;
1589 else if (ret == OK)
1590 {
1591 emsg(_(e_missing_closing_paren));
1592 ret = FAIL;
1593 }
1594 return ret;
1595}
1596
Bram Moolenaarc7349932022-01-16 20:59:39 +00001597static int compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
1598
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001599/*
1600 * Compile whatever comes after "name" or "name()".
1601 * Advances "*arg" only when something was recognized.
1602 */
1603 static int
1604compile_subscript(
1605 char_u **arg,
1606 cctx_T *cctx,
1607 char_u *start_leader,
1608 char_u **end_leader,
1609 ppconst_T *ppconst)
1610{
1611 char_u *name_start = *end_leader;
1612 int keeping_dict = FALSE;
1613
1614 for (;;)
1615 {
1616 char_u *p = skipwhite(*arg);
1617
1618 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1619 {
1620 char_u *next = peek_next_line_from_context(cctx);
1621
1622 // If a following line starts with "->{" or "->X" advance to that
1623 // line, so that a line break before "->" is allowed.
1624 // Also if a following line starts with ".x".
1625 if (next != NULL &&
1626 ((next[0] == '-' && next[1] == '>'
1627 && (next[2] == '{'
1628 || ASCII_ISALPHA(*skipwhite(next + 2))))
1629 || (next[0] == '.' && eval_isdictc(next[1]))))
1630 {
1631 next = next_line_from_context(cctx, TRUE);
1632 if (next == NULL)
1633 return FAIL;
1634 *arg = next;
1635 p = skipwhite(*arg);
1636 }
1637 }
1638
1639 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1640 // is not a function call.
1641 if (**arg == '(')
1642 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001643 type_T *type;
1644 int argcount = 0;
1645
1646 if (generate_ppconst(cctx, ppconst) == FAIL)
1647 return FAIL;
1648 ppconst->pp_is_const = FALSE;
1649
1650 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001651 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001652
1653 *arg = skipwhite(p + 1);
1654 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
1655 return FAIL;
1656 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1657 return FAIL;
1658 if (keeping_dict)
1659 {
1660 keeping_dict = FALSE;
1661 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1662 return FAIL;
1663 }
1664 }
1665 else if (*p == '-' && p[1] == '>')
1666 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001667 char_u *pstart = p;
1668 int alt;
1669 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001670
Bram Moolenaarc7349932022-01-16 20:59:39 +00001671 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001672 if (generate_ppconst(cctx, ppconst) == FAIL)
1673 return FAIL;
1674 ppconst->pp_is_const = FALSE;
1675
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001676 // Apply the '!', '-' and '+' first:
1677 // -1.0->func() works like (-1.0)->func()
1678 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1679 return FAIL;
1680
1681 p += 2;
1682 *arg = skipwhite(p);
1683 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001684
1685 // Three alternatives handled here:
1686 // 1. "base->name(" only a name, use compile_call()
1687 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1688 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001689 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001690 alt = 2;
1691 else
1692 {
1693 // alternative 1 or 3
1694 p = *arg;
1695 if (!eval_isnamec1(*p))
1696 {
1697 semsg(_(e_trailing_characters_str), pstart);
1698 return FAIL;
1699 }
1700 if (ASCII_ISALPHA(*p) && p[1] == ':')
1701 p += 2;
1702 for ( ; eval_isnamec(*p); ++p)
1703 ;
1704 if (*p == '(')
1705 {
1706 // alternative 1
1707 alt = 1;
1708 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1709 return FAIL;
1710 }
1711 else
1712 {
1713 // Must be alternative 3, find the "(". Only works within
1714 // one line.
1715 alt = 3;
1716 paren = vim_strchr(p, '(');
1717 if (paren == NULL)
1718 {
1719 semsg(_(e_missing_parenthesis_str), *arg);
1720 return FAIL;
1721 }
1722 }
1723 }
1724
1725 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001726 {
1727 int argcount = 1;
1728 garray_T *stack = &cctx->ctx_type_stack;
1729 int type_idx_start = stack->ga_len;
1730 type_T *type;
1731 int expr_isn_start = cctx->ctx_instr.ga_len;
1732 int expr_isn_end;
1733 int arg_isn_count;
1734
Bram Moolenaarc7349932022-01-16 20:59:39 +00001735 if (alt == 2)
1736 {
1737 // Funcref call: list->(Refs[2])(arg)
1738 // or lambda: list->((arg) => expr)(arg)
1739 //
1740 // Fist compile the function expression.
1741 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1742 return FAIL;
1743 }
1744 else
1745 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001746 int fail;
1747 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
1748
Bram Moolenaarc7349932022-01-16 20:59:39 +00001749 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001750
1751 // instead of using LOADG for "import.Func" use PUSHFUNC
1752 ++paren_follows_after_expr;
1753
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001754 // do not look in the next line
1755 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001756
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001757 fail = compile_expr8(arg, cctx, ppconst) == FAIL
1758 || *skipwhite(*arg) != NUL;
1759 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001760 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001761 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001762
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001763 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001764 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001765 semsg(_(e_invalid_expression_str), pstart);
1766 return FAIL;
1767 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001768 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001769
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001770 // Compile the arguments.
1771 if (**arg != '(')
1772 {
1773 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001774 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001775 else
1776 semsg(_(e_missing_parenthesis_str), *arg);
1777 return FAIL;
1778 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001779
1780 // Remember the next instruction index, where the instructions
1781 // for arguments are being written.
1782 expr_isn_end = cctx->ctx_instr.ga_len;
1783
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001784 *arg = skipwhite(*arg + 1);
1785 if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
1786 return FAIL;
1787
1788 // Move the instructions for the arguments to before the
1789 // instructions of the expression and move the type of the
1790 // expression after the argument types. This is what ISN_PCALL
1791 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001792 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
1793 if (arg_isn_count > 0)
1794 {
1795 int expr_isn_count = expr_isn_end - expr_isn_start;
1796 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001797 type_T *decl_type;
1798 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001799
1800 if (isn == NULL)
1801 return FAIL;
1802 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
1803 + expr_isn_start,
1804 sizeof(isn_T) * expr_isn_count);
1805 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1806 + expr_isn_start,
1807 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
1808 sizeof(isn_T) * arg_isn_count);
1809 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
1810 + expr_isn_start + arg_isn_count,
1811 isn, sizeof(isn_T) * expr_isn_count);
1812 vim_free(isn);
1813
Bram Moolenaar078a4612022-01-04 15:17:03 +00001814 typep = ((type2_T *)stack->ga_data) + type_idx_start;
1815 type = typep->type_curr;
1816 decl_type = typep->type_decl;
1817 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
1818 ((type2_T *)stack->ga_data) + type_idx_start + 1,
1819 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001820 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00001821 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
1822 typep->type_curr = type;
1823 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001824 }
1825
Bram Moolenaar078a4612022-01-04 15:17:03 +00001826 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001827 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
1828 return FAIL;
1829 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00001830
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001831 if (keeping_dict)
1832 {
1833 keeping_dict = FALSE;
1834 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1835 return FAIL;
1836 }
1837 }
1838 else if (**arg == '[')
1839 {
1840 int is_slice = FALSE;
1841
1842 // list index: list[123]
1843 // dict member: dict[key]
1844 // string index: text[123]
1845 // blob index: blob[123]
1846 if (generate_ppconst(cctx, ppconst) == FAIL)
1847 return FAIL;
1848 ppconst->pp_is_const = FALSE;
1849
1850 ++p;
1851 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1852 return FAIL;
1853 if (**arg == ':')
1854 {
1855 // missing first index is equal to zero
1856 generate_PUSHNR(cctx, 0);
1857 }
1858 else
1859 {
1860 if (compile_expr0(arg, cctx) == FAIL)
1861 return FAIL;
1862 if (**arg == ':')
1863 {
1864 semsg(_(e_white_space_required_before_and_after_str_at_str),
1865 ":", *arg);
1866 return FAIL;
1867 }
1868 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1869 return FAIL;
1870 *arg = skipwhite(*arg);
1871 }
1872 if (**arg == ':')
1873 {
1874 is_slice = TRUE;
1875 ++*arg;
1876 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
1877 {
1878 semsg(_(e_white_space_required_before_and_after_str_at_str),
1879 ":", *arg);
1880 return FAIL;
1881 }
1882 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1883 return FAIL;
1884 if (**arg == ']')
1885 // missing second index is equal to end of string
1886 generate_PUSHNR(cctx, -1);
1887 else
1888 {
1889 if (compile_expr0(arg, cctx) == FAIL)
1890 return FAIL;
1891 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1892 return FAIL;
1893 *arg = skipwhite(*arg);
1894 }
1895 }
1896
1897 if (**arg != ']')
1898 {
1899 emsg(_(e_missing_closing_square_brace));
1900 return FAIL;
1901 }
1902 *arg = *arg + 1;
1903
1904 if (keeping_dict)
1905 {
1906 keeping_dict = FALSE;
1907 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1908 return FAIL;
1909 }
1910 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
1911 return FAIL;
1912 }
1913 else if (*p == '.' && p[1] != '.')
1914 {
1915 // dictionary member: dict.name
1916 if (generate_ppconst(cctx, ppconst) == FAIL)
1917 return FAIL;
1918 ppconst->pp_is_const = FALSE;
1919
1920 *arg = p + 1;
1921 if (IS_WHITE_OR_NUL(**arg))
1922 {
1923 emsg(_(e_missing_name_after_dot));
1924 return FAIL;
1925 }
1926 p = *arg;
1927 if (eval_isdictc(*p))
1928 while (eval_isnamec(*p))
1929 MB_PTR_ADV(p);
1930 if (p == *arg)
1931 {
1932 semsg(_(e_syntax_error_at_str), *arg);
1933 return FAIL;
1934 }
1935 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
1936 return FAIL;
1937 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
1938 return FAIL;
1939 keeping_dict = TRUE;
1940 *arg = p;
1941 }
1942 else
1943 break;
1944 }
1945
1946 // Turn "dict.Func" into a partial for "Func" bound to "dict".
1947 // This needs to be done at runtime to be able to check the type.
1948 if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
1949 return FAIL;
1950
1951 return OK;
1952}
1953
1954/*
1955 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
1956 * "arg" is advanced until after the expression, skipping white space.
1957 *
1958 * If the value is a constant "ppconst->pp_used" will be non-zero.
1959 * Before instructions are generated, any values in "ppconst" will generated.
1960 *
1961 * This is the compiling equivalent of eval1(), eval2(), etc.
1962 */
1963
1964/*
1965 * number number constant
1966 * 0zFFFFFFFF Blob constant
1967 * "string" string constant
1968 * 'string' literal string constant
1969 * &option-name option value
1970 * @r register contents
1971 * identifier variable value
1972 * function() function call
1973 * $VAR environment variable
1974 * (expression) nested expression
1975 * [expr, expr] List
1976 * {key: val, [key]: val} Dictionary
1977 *
1978 * Also handle:
1979 * ! in front logical NOT
1980 * - in front unary minus
1981 * + in front unary plus (ignored)
1982 * trailing (arg) funcref/partial call
1983 * trailing [] subscript in String or List
1984 * trailing .name entry in Dictionary
1985 * trailing ->name() method call
1986 */
1987 static int
Bram Moolenaar5da36052021-12-27 15:39:57 +00001988compile_expr8(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001989 char_u **arg,
1990 cctx_T *cctx,
1991 ppconst_T *ppconst)
1992{
1993 char_u *start_leader, *end_leader;
1994 int ret = OK;
1995 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
1996 int used_before = ppconst->pp_used;
1997
1998 ppconst->pp_is_const = FALSE;
1999
2000 /*
2001 * Skip '!', '-' and '+' characters. They are handled later.
2002 */
2003 start_leader = *arg;
2004 if (eval_leader(arg, TRUE) == FAIL)
2005 return FAIL;
2006 end_leader = *arg;
2007
2008 rettv->v_type = VAR_UNKNOWN;
2009 switch (**arg)
2010 {
2011 /*
2012 * Number constant.
2013 */
2014 case '0': // also for blob starting with 0z
2015 case '1':
2016 case '2':
2017 case '3':
2018 case '4':
2019 case '5':
2020 case '6':
2021 case '7':
2022 case '8':
2023 case '9':
2024 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2025 return FAIL;
2026 // Apply "-" and "+" just before the number now, right to
2027 // left. Matters especially when "->" follows. Stops at
2028 // '!'.
2029 if (apply_leader(rettv, TRUE,
2030 start_leader, &end_leader) == FAIL)
2031 {
2032 clear_tv(rettv);
2033 return FAIL;
2034 }
2035 break;
2036
2037 /*
2038 * String constant: "string".
2039 */
2040 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
2041 return FAIL;
2042 break;
2043
2044 /*
2045 * Literal string constant: 'str''ing'.
2046 */
2047 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
2048 return FAIL;
2049 break;
2050
2051 /*
2052 * Constant Vim variable.
2053 */
2054 case 'v': get_vim_constant(arg, rettv);
2055 ret = NOTDONE;
2056 break;
2057
2058 /*
2059 * "true" constant
2060 */
2061 case 't': if (STRNCMP(*arg, "true", 4) == 0
2062 && !eval_isnamec((*arg)[4]))
2063 {
2064 *arg += 4;
2065 rettv->v_type = VAR_BOOL;
2066 rettv->vval.v_number = VVAL_TRUE;
2067 }
2068 else
2069 ret = NOTDONE;
2070 break;
2071
2072 /*
2073 * "false" constant
2074 */
2075 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2076 && !eval_isnamec((*arg)[5]))
2077 {
2078 *arg += 5;
2079 rettv->v_type = VAR_BOOL;
2080 rettv->vval.v_number = VVAL_FALSE;
2081 }
2082 else
2083 ret = NOTDONE;
2084 break;
2085
2086 /*
2087 * "null" constant
2088 */
2089 case 'n': if (STRNCMP(*arg, "null", 4) == 0
2090 && !eval_isnamec((*arg)[4]))
2091 {
2092 *arg += 4;
2093 rettv->v_type = VAR_SPECIAL;
2094 rettv->vval.v_number = VVAL_NULL;
2095 }
2096 else
2097 ret = NOTDONE;
2098 break;
2099
2100 /*
2101 * List: [expr, expr]
2102 */
2103 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2104 return FAIL;
2105 ret = compile_list(arg, cctx, ppconst);
2106 break;
2107
2108 /*
2109 * Dictionary: {'key': val, 'key': val}
2110 */
2111 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2112 return FAIL;
2113 ret = compile_dict(arg, cctx, ppconst);
2114 break;
2115
2116 /*
2117 * Option value: &name
2118 */
2119 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2120 return FAIL;
2121 ret = compile_get_option(arg, cctx);
2122 break;
2123
2124 /*
2125 * Environment variable: $VAR.
2126 */
2127 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2128 return FAIL;
2129 ret = compile_get_env(arg, cctx);
2130 break;
2131
2132 /*
2133 * Register contents: @r.
2134 */
2135 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2136 return FAIL;
2137 ret = compile_get_register(arg, cctx);
2138 break;
2139 /*
2140 * nested expression: (expression).
2141 * lambda: (arg, arg) => expr
2142 * funcref: (arg, arg) => { statement }
2143 */
2144 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2145 ret = compile_lambda(arg, cctx);
2146 if (ret == NOTDONE)
2147 ret = compile_parenthesis(arg, cctx, ppconst);
2148 break;
2149
2150 default: ret = NOTDONE;
2151 break;
2152 }
2153 if (ret == FAIL)
2154 return FAIL;
2155
2156 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2157 {
2158 if (cctx->ctx_skip == SKIP_YES)
2159 clear_tv(rettv);
2160 else
2161 // A constant expression can possibly be handled compile time,
2162 // return the value instead of generating code.
2163 ++ppconst->pp_used;
2164 }
2165 else if (ret == NOTDONE)
2166 {
2167 char_u *p;
2168 int r;
2169
2170 if (!eval_isnamec1(**arg))
2171 {
2172 if (!vim9_bad_comment(*arg))
2173 {
2174 if (ends_excmd(*skipwhite(*arg)))
2175 semsg(_(e_empty_expression_str), *arg);
2176 else
2177 semsg(_(e_name_expected_str), *arg);
2178 }
2179 return FAIL;
2180 }
2181
2182 // "name" or "name()"
2183 p = to_name_end(*arg, TRUE);
2184 if (p - *arg == (size_t)1 && **arg == '_')
2185 {
2186 emsg(_(e_cannot_use_underscore_here));
2187 return FAIL;
2188 }
2189
2190 if (*p == '(')
2191 {
2192 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2193 }
2194 else
2195 {
2196 if (cctx->ctx_skip != SKIP_YES
2197 && generate_ppconst(cctx, ppconst) == FAIL)
2198 return FAIL;
2199 r = compile_load(arg, p, cctx, TRUE, TRUE);
2200 }
2201 if (r == FAIL)
2202 return FAIL;
2203 }
2204
2205 // Handle following "[]", ".member", etc.
2206 // Then deal with prefixed '-', '+' and '!', if not done already.
2207 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2208 ppconst) == FAIL)
2209 return FAIL;
2210 if (ppconst->pp_used > 0)
2211 {
2212 // apply the '!', '-' and '+' before the constant
2213 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2214 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2215 return FAIL;
2216 return OK;
2217 }
2218 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2219 return FAIL;
2220 return OK;
2221}
2222
2223/*
Bram Moolenaar5da36052021-12-27 15:39:57 +00002224 * <type>expr8: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002225 */
2226 static int
Bram Moolenaar5da36052021-12-27 15:39:57 +00002227compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002228{
2229 type_T *want_type = NULL;
2230
2231 // Recognize <type>
2232 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2233 {
2234 ++*arg;
2235 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2236 if (want_type == NULL)
2237 return FAIL;
2238
2239 if (**arg != '>')
2240 {
2241 if (*skipwhite(*arg) == '>')
2242 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2243 else
2244 emsg(_(e_missing_gt));
2245 return FAIL;
2246 }
2247 ++*arg;
2248 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2249 return FAIL;
2250 }
2251
Bram Moolenaar5da36052021-12-27 15:39:57 +00002252 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002253 return FAIL;
2254
2255 if (want_type != NULL)
2256 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002257 type_T *actual;
2258 where_T where = WHERE_INIT;
2259
2260 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002261 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002262 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002263 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002264 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2265 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002266 return FAIL;
2267 }
2268 }
2269
2270 return OK;
2271}
2272
2273/*
2274 * * number multiplication
2275 * / number division
2276 * % number modulo
2277 */
2278 static int
2279compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2280{
2281 char_u *op;
2282 char_u *next;
2283 int ppconst_used = ppconst->pp_used;
2284
2285 // get the first expression
Bram Moolenaar5da36052021-12-27 15:39:57 +00002286 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002287 return FAIL;
2288
2289 /*
2290 * Repeat computing, until no "*", "/" or "%" is following.
2291 */
2292 for (;;)
2293 {
2294 op = may_peek_next_line(cctx, *arg, &next);
2295 if (*op != '*' && *op != '/' && *op != '%')
2296 break;
2297 if (next != NULL)
2298 {
2299 *arg = next_line_from_context(cctx, TRUE);
2300 op = skipwhite(*arg);
2301 }
2302
2303 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2304 {
2305 error_white_both(op, 1);
2306 return FAIL;
2307 }
2308 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2309 return FAIL;
2310
2311 // get the second expression
Bram Moolenaar5da36052021-12-27 15:39:57 +00002312 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002313 return FAIL;
2314
2315 if (ppconst->pp_used == ppconst_used + 2
2316 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2317 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2318 {
2319 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2320 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2321 varnumber_T res = 0;
2322 int failed = FALSE;
2323
2324 // both are numbers: compute the result
2325 switch (*op)
2326 {
2327 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2328 break;
2329 case '/': res = num_divide(tv1->vval.v_number,
2330 tv2->vval.v_number, &failed);
2331 break;
2332 case '%': res = num_modulus(tv1->vval.v_number,
2333 tv2->vval.v_number, &failed);
2334 break;
2335 }
2336 if (failed)
2337 return FAIL;
2338 tv1->vval.v_number = res;
2339 --ppconst->pp_used;
2340 }
2341 else
2342 {
2343 generate_ppconst(cctx, ppconst);
2344 generate_two_op(cctx, op);
2345 }
2346 }
2347
2348 return OK;
2349}
2350
2351/*
2352 * + number addition or list/blobl concatenation
2353 * - number subtraction
2354 * .. string concatenation
2355 */
2356 static int
2357compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2358{
2359 char_u *op;
2360 char_u *next;
2361 int oplen;
2362 int ppconst_used = ppconst->pp_used;
2363
2364 // get the first variable
2365 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2366 return FAIL;
2367
2368 /*
2369 * Repeat computing, until no "+", "-" or ".." is following.
2370 */
2371 for (;;)
2372 {
2373 op = may_peek_next_line(cctx, *arg, &next);
2374 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2375 break;
2376 if (op[0] == op[1] && *op != '.' && next)
2377 // Finding "++" or "--" on the next line is a separate command.
2378 // But ".." is concatenation.
2379 break;
2380 oplen = (*op == '.' ? 2 : 1);
2381 if (next != NULL)
2382 {
2383 *arg = next_line_from_context(cctx, TRUE);
2384 op = skipwhite(*arg);
2385 }
2386
2387 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2388 {
2389 error_white_both(op, oplen);
2390 return FAIL;
2391 }
2392
2393 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2394 return FAIL;
2395
2396 // get the second expression
2397 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2398 return FAIL;
2399
2400 if (ppconst->pp_used == ppconst_used + 2
2401 && (*op == '.'
2402 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2403 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2404 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2405 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2406 {
2407 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2408 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2409
2410 // concat/subtract/add constant numbers
2411 if (*op == '+')
2412 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2413 else if (*op == '-')
2414 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2415 else
2416 {
2417 // concatenate constant strings
2418 char_u *s1 = tv1->vval.v_string;
2419 char_u *s2 = tv2->vval.v_string;
2420 size_t len1 = STRLEN(s1);
2421
2422 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2423 if (tv1->vval.v_string == NULL)
2424 {
2425 clear_ppconst(ppconst);
2426 return FAIL;
2427 }
2428 mch_memmove(tv1->vval.v_string, s1, len1);
2429 STRCPY(tv1->vval.v_string + len1, s2);
2430 vim_free(s1);
2431 vim_free(s2);
2432 }
2433 --ppconst->pp_used;
2434 }
2435 else
2436 {
2437 generate_ppconst(cctx, ppconst);
2438 ppconst->pp_is_const = FALSE;
2439 if (*op == '.')
2440 {
2441 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2442 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2443 return FAIL;
2444 generate_instr_drop(cctx, ISN_CONCAT, 1);
2445 }
2446 else
2447 generate_two_op(cctx, op);
2448 }
2449 }
2450
2451 return OK;
2452}
2453
2454/*
2455 * expr5a == expr5b
2456 * expr5a =~ expr5b
2457 * expr5a != expr5b
2458 * expr5a !~ expr5b
2459 * expr5a > expr5b
2460 * expr5a >= expr5b
2461 * expr5a < expr5b
2462 * expr5a <= expr5b
2463 * expr5a is expr5b
2464 * expr5a isnot expr5b
2465 *
2466 * Produces instructions:
2467 * EVAL expr5a Push result of "expr5a"
2468 * EVAL expr5b Push result of "expr5b"
2469 * COMPARE one of the compare instructions
2470 */
2471 static int
2472compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2473{
2474 exprtype_T type = EXPR_UNKNOWN;
2475 char_u *p;
2476 char_u *next;
2477 int len = 2;
2478 int type_is = FALSE;
2479 int ppconst_used = ppconst->pp_used;
2480
2481 // get the first variable
2482 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2483 return FAIL;
2484
2485 p = may_peek_next_line(cctx, *arg, &next);
2486 type = get_compare_type(p, &len, &type_is);
2487
2488 /*
2489 * If there is a comparative operator, use it.
2490 */
2491 if (type != EXPR_UNKNOWN)
2492 {
2493 int ic = FALSE; // Default: do not ignore case
2494
2495 if (next != NULL)
2496 {
2497 *arg = next_line_from_context(cctx, TRUE);
2498 p = skipwhite(*arg);
2499 }
2500 if (type_is && (p[len] == '?' || p[len] == '#'))
2501 {
2502 semsg(_(e_invalid_expression_str), *arg);
2503 return FAIL;
2504 }
2505 // extra question mark appended: ignore case
2506 if (p[len] == '?')
2507 {
2508 ic = TRUE;
2509 ++len;
2510 }
2511 // extra '#' appended: match case (ignored)
2512 else if (p[len] == '#')
2513 ++len;
2514 // nothing appended: match case
2515
2516 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2517 {
2518 error_white_both(p, len);
2519 return FAIL;
2520 }
2521
2522 // get the second variable
2523 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2524 return FAIL;
2525
2526 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2527 return FAIL;
2528
2529 if (ppconst->pp_used == ppconst_used + 2)
2530 {
2531 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2532 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2533 int ret;
2534
2535 // Both sides are a constant, compute the result now.
2536 // First check for a valid combination of types, this is more
2537 // strict than typval_compare().
2538 if (check_compare_types(type, tv1, tv2) == FAIL)
2539 ret = FAIL;
2540 else
2541 {
2542 ret = typval_compare(tv1, tv2, type, ic);
2543 tv1->v_type = VAR_BOOL;
2544 tv1->vval.v_number = tv1->vval.v_number
2545 ? VVAL_TRUE : VVAL_FALSE;
2546 clear_tv(tv2);
2547 --ppconst->pp_used;
2548 }
2549 return ret;
2550 }
2551
2552 generate_ppconst(cctx, ppconst);
2553 return generate_COMPARE(cctx, type, ic);
2554 }
2555
2556 return OK;
2557}
2558
2559static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2560
2561/*
2562 * Compile || or &&.
2563 */
2564 static int
2565compile_and_or(
2566 char_u **arg,
2567 cctx_T *cctx,
2568 char *op,
2569 ppconst_T *ppconst,
2570 int ppconst_used UNUSED)
2571{
2572 char_u *next;
2573 char_u *p = may_peek_next_line(cctx, *arg, &next);
2574 int opchar = *op;
2575
2576 if (p[0] == opchar && p[1] == opchar)
2577 {
2578 garray_T *instr = &cctx->ctx_instr;
2579 garray_T end_ga;
2580 int save_skip = cctx->ctx_skip;
2581
2582 /*
2583 * Repeat until there is no following "||" or "&&"
2584 */
2585 ga_init2(&end_ga, sizeof(int), 10);
2586 while (p[0] == opchar && p[1] == opchar)
2587 {
2588 long start_lnum = SOURCING_LNUM;
2589 long save_sourcing_lnum;
2590 int start_ctx_lnum = cctx->ctx_lnum;
2591 int save_lnum;
2592 int const_used;
2593 int status;
2594 jumpwhen_T jump_when = opchar == '|'
2595 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2596
2597 if (next != NULL)
2598 {
2599 *arg = next_line_from_context(cctx, TRUE);
2600 p = skipwhite(*arg);
2601 }
2602
2603 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2604 {
2605 semsg(_(e_white_space_required_before_and_after_str_at_str),
2606 op, p);
2607 ga_clear(&end_ga);
2608 return FAIL;
2609 }
2610
2611 save_sourcing_lnum = SOURCING_LNUM;
2612 SOURCING_LNUM = start_lnum;
2613 save_lnum = cctx->ctx_lnum;
2614 cctx->ctx_lnum = start_ctx_lnum;
2615
2616 status = check_ppconst_bool(ppconst);
2617 if (status != FAIL)
2618 {
2619 // Use the last ppconst if possible.
2620 if (ppconst->pp_used > 0)
2621 {
2622 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2623 int is_true = tv2bool(tv);
2624
2625 if ((is_true && opchar == '|')
2626 || (!is_true && opchar == '&'))
2627 {
2628 // For "false && expr" and "true || expr" the "expr"
2629 // does not need to be evaluated.
2630 cctx->ctx_skip = SKIP_YES;
2631 clear_tv(tv);
2632 tv->v_type = VAR_BOOL;
2633 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
2634 }
2635 else
2636 {
2637 // For "true && expr" and "false || expr" only "expr"
2638 // needs to be evaluated.
2639 --ppconst->pp_used;
2640 jump_when = JUMP_NEVER;
2641 }
2642 }
2643 else
2644 {
2645 // Every part must evaluate to a bool.
2646 status = bool_on_stack(cctx);
2647 }
2648 }
2649 if (status != FAIL)
2650 status = ga_grow(&end_ga, 1);
2651 cctx->ctx_lnum = save_lnum;
2652 if (status == FAIL)
2653 {
2654 ga_clear(&end_ga);
2655 return FAIL;
2656 }
2657
2658 if (jump_when != JUMP_NEVER)
2659 {
2660 if (cctx->ctx_skip != SKIP_YES)
2661 {
2662 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
2663 ++end_ga.ga_len;
2664 }
2665 generate_JUMP(cctx, jump_when, 0);
2666 }
2667
2668 // eval the next expression
2669 SOURCING_LNUM = save_sourcing_lnum;
2670 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
2671 {
2672 ga_clear(&end_ga);
2673 return FAIL;
2674 }
2675
2676 const_used = ppconst->pp_used;
2677 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
2678 : compile_expr4(arg, cctx, ppconst)) == FAIL)
2679 {
2680 ga_clear(&end_ga);
2681 return FAIL;
2682 }
2683
2684 // "0 || 1" results in true, "1 && 0" results in false.
2685 if (ppconst->pp_used == const_used + 1)
2686 {
2687 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
2688
2689 if (tv->v_type == VAR_NUMBER
2690 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
2691 {
2692 tv->vval.v_number = tv->vval.v_number == 1
2693 ? VVAL_TRUE : VVAL_FALSE;
2694 tv->v_type = VAR_BOOL;
2695 }
2696 }
2697
2698 p = may_peek_next_line(cctx, *arg, &next);
2699 }
2700
2701 if (check_ppconst_bool(ppconst) == FAIL)
2702 {
2703 ga_clear(&end_ga);
2704 return FAIL;
2705 }
2706
2707 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
2708 // Every part must evaluate to a bool.
2709 if (bool_on_stack(cctx) == FAIL)
2710 {
2711 ga_clear(&end_ga);
2712 return FAIL;
2713 }
2714
2715 if (end_ga.ga_len > 0)
2716 {
2717 // Fill in the end label in all jumps.
2718 generate_ppconst(cctx, ppconst);
2719 while (end_ga.ga_len > 0)
2720 {
2721 isn_T *isn;
2722
2723 --end_ga.ga_len;
2724 isn = ((isn_T *)instr->ga_data)
2725 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
2726 isn->isn_arg.jump.jump_where = instr->ga_len;
2727 }
2728 }
2729 ga_clear(&end_ga);
2730
2731 cctx->ctx_skip = save_skip;
2732 }
2733
2734 return OK;
2735}
2736
2737/*
2738 * expr4a && expr4a && expr4a logical AND
2739 *
2740 * Produces instructions:
2741 * EVAL expr4a Push result of "expr4a"
2742 * COND2BOOL convert to bool if needed
2743 * JUMP_IF_COND_FALSE end
2744 * EVAL expr4b Push result of "expr4b"
2745 * JUMP_IF_COND_FALSE end
2746 * EVAL expr4c Push result of "expr4c"
2747 * end:
2748 */
2749 static int
2750compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2751{
2752 int ppconst_used = ppconst->pp_used;
2753
2754 // get the first variable
2755 if (compile_expr4(arg, cctx, ppconst) == FAIL)
2756 return FAIL;
2757
2758 // || and && work almost the same
2759 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
2760}
2761
2762/*
2763 * expr3a || expr3b || expr3c logical OR
2764 *
2765 * Produces instructions:
2766 * EVAL expr3a Push result of "expr3a"
2767 * COND2BOOL convert to bool if needed
2768 * JUMP_IF_COND_TRUE end
2769 * EVAL expr3b Push result of "expr3b"
2770 * JUMP_IF_COND_TRUE end
2771 * EVAL expr3c Push result of "expr3c"
2772 * end:
2773 */
2774 static int
2775compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2776{
2777 int ppconst_used = ppconst->pp_used;
2778
2779 // eval the first expression
2780 if (compile_expr3(arg, cctx, ppconst) == FAIL)
2781 return FAIL;
2782
2783 // || and && work almost the same
2784 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
2785}
2786
2787/*
2788 * Toplevel expression: expr2 ? expr1a : expr1b
2789 * Produces instructions:
2790 * EVAL expr2 Push result of "expr2"
2791 * JUMP_IF_FALSE alt jump if false
2792 * EVAL expr1a
2793 * JUMP_ALWAYS end
2794 * alt: EVAL expr1b
2795 * end:
2796 *
2797 * Toplevel expression: expr2 ?? expr1
2798 * Produces instructions:
2799 * EVAL expr2 Push result of "expr2"
2800 * JUMP_AND_KEEP_IF_TRUE end jump if true
2801 * EVAL expr1
2802 * end:
2803 */
2804 int
2805compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2806{
2807 char_u *p;
2808 int ppconst_used = ppconst->pp_used;
2809 char_u *next;
2810
2811 // Ignore all kinds of errors when not producing code.
2812 if (cctx->ctx_skip == SKIP_YES)
2813 {
2814 skip_expr_cctx(arg, cctx);
2815 return OK;
2816 }
2817
2818 // Evaluate the first expression.
2819 if (compile_expr2(arg, cctx, ppconst) == FAIL)
2820 return FAIL;
2821
2822 p = may_peek_next_line(cctx, *arg, &next);
2823 if (*p == '?')
2824 {
2825 int op_falsy = p[1] == '?';
2826 garray_T *instr = &cctx->ctx_instr;
2827 garray_T *stack = &cctx->ctx_type_stack;
2828 int alt_idx = instr->ga_len;
2829 int end_idx = 0;
2830 isn_T *isn;
2831 type_T *type1 = NULL;
2832 int has_const_expr = FALSE;
2833 int const_value = FALSE;
2834 int save_skip = cctx->ctx_skip;
2835
2836 if (next != NULL)
2837 {
2838 *arg = next_line_from_context(cctx, TRUE);
2839 p = skipwhite(*arg);
2840 }
2841
2842 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
2843 {
2844 semsg(_(e_white_space_required_before_and_after_str_at_str),
2845 op_falsy ? "??" : "?", p);
2846 return FAIL;
2847 }
2848
2849 if (ppconst->pp_used == ppconst_used + 1)
2850 {
2851 // the condition is a constant, we know whether the ? or the :
2852 // expression is to be evaluated.
2853 has_const_expr = TRUE;
2854 if (op_falsy)
2855 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
2856 else
2857 {
2858 int error = FALSE;
2859
2860 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
2861 &error);
2862 if (error)
2863 return FAIL;
2864 }
2865 cctx->ctx_skip = save_skip == SKIP_YES ||
2866 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
2867
2868 if (op_falsy && cctx->ctx_skip == SKIP_YES)
2869 // "left ?? right" and "left" is truthy: produce "left"
2870 generate_ppconst(cctx, ppconst);
2871 else
2872 {
2873 clear_tv(&ppconst->pp_tv[ppconst_used]);
2874 --ppconst->pp_used;
2875 }
2876 }
2877 else
2878 {
2879 generate_ppconst(cctx, ppconst);
2880 if (op_falsy)
2881 end_idx = instr->ga_len;
2882 generate_JUMP(cctx, op_falsy
2883 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
2884 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00002885 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002886 }
2887
2888 // evaluate the second expression; any type is accepted
2889 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
2890 return FAIL;
2891 if (compile_expr1(arg, cctx, ppconst) == FAIL)
2892 return FAIL;
2893
2894 if (!has_const_expr)
2895 {
2896 generate_ppconst(cctx, ppconst);
2897
2898 if (!op_falsy)
2899 {
2900 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00002901 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002902 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002903
2904 end_idx = instr->ga_len;
2905 generate_JUMP(cctx, JUMP_ALWAYS, 0);
2906
2907 // jump here from JUMP_IF_FALSE
2908 isn = ((isn_T *)instr->ga_data) + alt_idx;
2909 isn->isn_arg.jump.jump_where = instr->ga_len;
2910 }
2911 }
2912
2913 if (!op_falsy)
2914 {
2915 // Check for the ":".
2916 p = may_peek_next_line(cctx, *arg, &next);
2917 if (*p != ':')
2918 {
2919 emsg(_(e_missing_colon_after_questionmark));
2920 return FAIL;
2921 }
2922 if (next != NULL)
2923 {
2924 *arg = next_line_from_context(cctx, TRUE);
2925 p = skipwhite(*arg);
2926 }
2927
2928 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
2929 {
2930 semsg(_(e_white_space_required_before_and_after_str_at_str),
2931 ":", p);
2932 return FAIL;
2933 }
2934
2935 // evaluate the third expression
2936 if (has_const_expr)
2937 cctx->ctx_skip = save_skip == SKIP_YES || const_value
2938 ? SKIP_YES : SKIP_NOT;
2939 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
2940 return FAIL;
2941 if (compile_expr1(arg, cctx, ppconst) == FAIL)
2942 return FAIL;
2943 }
2944
2945 if (!has_const_expr)
2946 {
2947 type_T **typep;
2948
2949 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00002950 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002951
2952 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00002953 typep = &((((type2_T *)stack->ga_data)
2954 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002955 common_type(type1, *typep, typep, cctx->ctx_type_list);
2956
2957 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
2958 isn = ((isn_T *)instr->ga_data) + end_idx;
2959 isn->isn_arg.jump.jump_where = instr->ga_len;
2960 }
2961
2962 cctx->ctx_skip = save_skip;
2963 }
2964 return OK;
2965}
2966
2967/*
2968 * Toplevel expression.
2969 * Sets "is_const" (if not NULL) to indicate the value is a constant.
2970 * Returns OK or FAIL.
2971 */
2972 int
2973compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
2974{
2975 ppconst_T ppconst;
2976
2977 CLEAR_FIELD(ppconst);
2978 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
2979 {
2980 clear_ppconst(&ppconst);
2981 return FAIL;
2982 }
2983 if (is_const != NULL)
2984 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
2985 if (generate_ppconst(cctx, &ppconst) == FAIL)
2986 return FAIL;
2987 return OK;
2988}
2989
2990/*
2991 * Toplevel expression.
2992 */
2993 int
2994compile_expr0(char_u **arg, cctx_T *cctx)
2995{
2996 return compile_expr0_ext(arg, cctx, NULL);
2997}
2998
2999
3000#endif // defined(FEAT_EVAL)