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