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