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