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