blob: 27f3bc465f4c626bd02fb9b7bd2adb20316583c3 [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/*
Kota Kato948a3892022-08-16 16:09:59 +010011 * vim9expr.c: Dealing with compiled function expressions
Bram Moolenaardc7c3662021-12-20 15:04:29 +000012 */
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 Moolenaar4913d422022-10-17 13:13:32 +010096 if ((typep->type_curr->tt_type == VAR_ANY
97 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaar078a4612022-01-04 15:17:03 +000098 && idxtype == &t_string)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000099 vartype = VAR_DICT;
100 if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
101 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000102 if (need_type(idxtype, &t_number, FALSE,
103 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000104 return FAIL;
105 if (is_slice)
106 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000107 idxtype = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000108 if (need_type(idxtype, &t_number, FALSE,
109 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000110 return FAIL;
111 }
112 }
113
114 if (vartype == VAR_DICT)
115 {
116 if (is_slice)
117 {
118 emsg(_(e_cannot_slice_dictionary));
119 return FAIL;
120 }
Bram Moolenaar078a4612022-01-04 15:17:03 +0000121 if (typep->type_curr->tt_type == VAR_DICT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000122 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000123 typep->type_curr = typep->type_curr->tt_member;
124 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000125 // empty dict was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000126 typep->type_curr = &t_any;
127 if (typep->type_decl->tt_type == VAR_DICT)
128 {
129 typep->type_decl = typep->type_decl->tt_member;
130 if (typep->type_decl == &t_unknown)
131 // empty dict was used
132 typep->type_decl = &t_any;
133 }
134 else
135 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000136 }
137 else
138 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000139 if (need_type(typep->type_curr, &t_dict_any, FALSE,
140 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000141 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000142 typep->type_curr = &t_any;
143 typep->type_decl = &t_any;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000144 }
Bram Moolenaard0132f42022-05-12 11:05:40 +0100145 if (may_generate_2STRING(-1, FALSE, cctx) == FAIL
146 || generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000147 return FAIL;
148 if (keeping_dict != NULL)
149 *keeping_dict = TRUE;
150 }
151 else if (vartype == VAR_STRING)
152 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000153 typep->type_curr = &t_string;
154 typep->type_decl = &t_string;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000155 if ((is_slice
156 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
157 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
158 return FAIL;
159 }
160 else if (vartype == VAR_BLOB)
161 {
162 if (is_slice)
163 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000164 typep->type_curr = &t_blob;
165 typep->type_decl = &t_blob;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000166 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
167 return FAIL;
168 }
169 else
170 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000171 typep->type_curr = &t_number;
172 typep->type_decl = &t_number;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000173 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
174 return FAIL;
175 }
176 }
Bram Moolenaar4913d422022-10-17 13:13:32 +0100177 else if (vartype == VAR_LIST || typep->type_curr->tt_type == VAR_ANY
178 || typep->type_curr->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000179 {
180 if (is_slice)
181 {
182 if (generate_instr_drop(cctx,
183 vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
184 2) == FAIL)
185 return FAIL;
Bram Moolenaar5f4ef5f2022-02-06 18:36:53 +0000186 // a copy is made so the member type is no longer declared
187 if (typep->type_decl->tt_type == VAR_LIST)
188 typep->type_decl = &t_list_any;
Bram Moolenaaradbc08f2022-11-06 18:27:17 +0000189
190 // a copy is made, the composite is no longer "const"
191 if (typep->type_curr->tt_flags & TTFLAG_CONST)
192 {
193 type_T *type = copy_type(typep->type_curr, cctx->ctx_type_list);
194
195 if (type != typep->type_curr) // did get a copy
196 {
197 type->tt_flags &= ~(TTFLAG_CONST | TTFLAG_STATIC);
198 typep->type_curr = type;
199 }
200 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000201 }
202 else
203 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000204 if (typep->type_curr->tt_type == VAR_LIST)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000205 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000206 typep->type_curr = typep->type_curr->tt_member;
207 if (typep->type_curr == &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000208 // empty list was used
Bram Moolenaar078a4612022-01-04 15:17:03 +0000209 typep->type_curr = &t_any;
210 if (typep->type_decl->tt_type == VAR_LIST)
211 {
212 typep->type_decl = typep->type_decl->tt_member;
213 if (typep->type_decl == &t_unknown)
214 // empty list was used
215 typep->type_decl = &t_any;
216 }
217 else
218 typep->type_decl = typep->type_curr;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000219 }
220 if (generate_instr_drop(cctx,
221 vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
222 == FAIL)
223 return FAIL;
224 }
225 }
226 else
227 {
228 switch (vartype)
229 {
230 case VAR_FUNC:
231 case VAR_PARTIAL:
232 emsg(_(e_cannot_index_a_funcref));
233 break;
234 case VAR_BOOL:
235 case VAR_SPECIAL:
236 case VAR_JOB:
237 case VAR_CHANNEL:
238 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000239 case VAR_CLASS:
240 case VAR_OBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000241 case VAR_UNKNOWN:
242 case VAR_ANY:
243 case VAR_VOID:
244 emsg(_(e_cannot_index_special_variable));
245 break;
246 default:
247 emsg(_(e_string_list_dict_or_blob_required));
248 }
249 return FAIL;
250 }
251 return OK;
252}
253
254/*
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000255 * Compile ".member" coming after an object or class.
256 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000257 static int
258compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
259{
260 if (VIM_ISWHITE((*arg)[1]))
261 {
262 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
263 return FAIL;
264 }
265
Bram Moolenaar58b40092023-01-11 15:59:05 +0000266 class_T *cl = (class_T *)type->tt_member;
267 int is_super = type->tt_flags & TTFLAG_SUPER;
268 if (type == &t_super)
269 {
270 if (cctx->ctx_ufunc == NULL || cctx->ctx_ufunc->uf_class == NULL)
271 emsg(_(e_using_super_not_in_class_function));
272 else
273 {
274 is_super = TRUE;
275 cl = cctx->ctx_ufunc->uf_class;
276 // Remove &t_super from the stack.
277 --cctx->ctx_type_stack.ga_len;
278 }
279 }
280 else if (type->tt_type == VAR_CLASS)
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000281 {
282 garray_T *instr = &cctx->ctx_instr;
283 if (instr->ga_len > 0)
284 {
285 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
286 if (isn->isn_type == ISN_LOADSCRIPT)
287 {
288 // The class was recognized as a script item. We only need
289 // to know what class it is, drop the instruction.
290 --instr->ga_len;
291 vim_free(isn->isn_arg.script.scriptref);
292 }
293 }
294 }
295
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000296 ++*arg;
297 char_u *name = *arg;
298 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
299 if (name_end == name)
300 return FAIL;
301 size_t len = name_end - name;
302
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000303 if (*name_end == '(')
304 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000305 int function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000306 int child_count;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000307 ufunc_T **functions;
308
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000309 if (type->tt_type == VAR_CLASS)
310 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000311 function_count = cl->class_class_function_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000312 child_count = cl->class_class_function_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000313 functions = cl->class_class_functions;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000314 }
315 else
316 {
Bram Moolenaar574950d2023-01-03 19:08:50 +0000317 // type->tt_type == VAR_OBJECT: method call
318 function_count = cl->class_obj_method_count;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000319 child_count = cl->class_obj_method_count_child;
Bram Moolenaar574950d2023-01-03 19:08:50 +0000320 functions = cl->class_obj_methods;
Bram Moolenaar46ab9252023-01-03 14:01:21 +0000321 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000322
323 ufunc_T *ufunc = NULL;
Bram Moolenaar58b40092023-01-11 15:59:05 +0000324 for (int i = is_super ? child_count : 0; i < function_count; ++i)
Bram Moolenaar574950d2023-01-03 19:08:50 +0000325 {
326 ufunc_T *fp = functions[i];
327 // Use a separate pointer to avoid that ASAN complains about
328 // uf_name[] only being 4 characters.
329 char_u *ufname = (char_u *)fp->uf_name;
330 if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
331 {
332 ufunc = fp;
333 break;
334 }
335 }
336 if (ufunc == NULL)
337 {
338 // TODO: different error for object method?
339 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
340 return FAIL;
341 }
342
343 // Compile the arguments and call the class function or object method.
344 // The object method will know that the object is on the stack, just
345 // before the arguments.
346 *arg = skipwhite(name_end + 1);
347 int argcount = 0;
348 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
349 return FAIL;
350 return generate_CALL(cctx, ufunc, argcount);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000351 }
Bram Moolenaar574950d2023-01-03 19:08:50 +0000352
353 if (type->tt_type == VAR_OBJECT)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000354 {
355 for (int i = 0; i < cl->class_obj_member_count; ++i)
356 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000357 ocmember_T *m = &cl->class_obj_members[i];
358 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000359 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000360 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
361 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000362 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000363 return FAIL;
364 }
365
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000366 *arg = name_end;
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000367 return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000368 }
369 }
370
371 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
372 }
373 else
374 {
Bram Moolenaar3259ff32023-01-04 18:54:09 +0000375 // load class member
376 int idx;
377 for (idx = 0; idx < cl->class_class_member_count; ++idx)
378 {
379 ocmember_T *m = &cl->class_class_members[idx];
380 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
381 break;
382 }
383 if (idx < cl->class_class_member_count)
384 {
385 *arg = name_end;
386 return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
387 }
388 semsg(_(e_class_member_not_found_str), name);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000389 }
390
391 return FAIL;
392}
393
394/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000395 * Generate an instruction to load script-local variable "name", without the
396 * leading "s:".
397 * Also finds imported variables.
398 */
399 int
400compile_load_scriptvar(
401 cctx_T *cctx,
402 char_u *name, // variable NUL terminated
403 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100404 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000405{
406 scriptitem_T *si;
407 int idx;
408 imported_T *import;
409
410 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
411 return FAIL;
412 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000413 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000414 if (idx >= 0)
415 {
416 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
417
418 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
419 current_sctx.sc_sid, idx, sv->sv_type);
420 return OK;
421 }
422
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000423 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000424 if (import != NULL)
425 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000426 char_u *p = skipwhite(*end);
427 char_u *exp_name;
428 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100429 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000430 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000431 int done = FALSE;
432 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000433
434 // Need to lookup the member.
435 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000436 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000437 semsg(_(e_expected_dot_after_name_str), start);
438 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000439 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000440 ++p;
441 if (VIM_ISWHITE(*p))
442 {
443 emsg(_(e_no_white_space_allowed_after_dot));
444 return FAIL;
445 }
446
447 // isolate one name
448 exp_name = p;
449 while (eval_isnamec(*p))
450 ++p;
451 cc = *p;
452 *p = NUL;
453
Bram Moolenaard041f422022-01-12 19:54:00 +0000454 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100455 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
456 // "import autoload './dir/script.vim'" or
457 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100458 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100459
460 if (res == OK)
461 {
462 if (si->sn_autoload_prefix != NULL
463 && si->sn_state == SN_STATE_NOT_LOADED)
464 {
465 char_u *auto_name =
466 concat_str(si->sn_autoload_prefix, exp_name);
467
468 // autoload script must be loaded later, access by the autoload
469 // name. If a '(' follows it must be a function. Otherwise we
470 // don't know, it can be "script.Func".
471 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100472 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100473 else
474 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
475 vim_free(auto_name);
476 done = TRUE;
477 }
478 else if (si->sn_import_autoload
479 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100480 {
481 // If a '(' follows it must be a function. Otherwise we don't
482 // know, it can be "script.Func".
483 if (cc == '(' || paren_follows_after_expr)
484 {
485 char_u sid_name[MAX_FUNC_NAME_LEN];
486
487 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100488 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100489 }
490 else
491 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
492 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100493 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100494 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100495 else
496 {
497 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
498 cctx, NULL, TRUE);
499 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100500 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100501
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000502 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000503 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000504 if (done)
505 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000506
507 if (idx < 0)
508 {
509 if (ufunc != NULL)
510 {
511 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100512 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000513 return OK;
514 }
515 return FAIL;
516 }
517
518 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
519 import->imp_sid,
520 idx,
521 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000522 return OK;
523 }
524
Bram Moolenaard0132f42022-05-12 11:05:40 +0100525 // Can only get here if we know "name" is a script variable and not in a
526 // Vim9 script (variable is not in sn_var_vals): old style script.
527 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000528 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000529}
530
531 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000532generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000533{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000534 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000535 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000536
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000537 // Reject a global non-autoload function found without the "g:" prefix.
538 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000539 return FAIL;
540
541 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000542 compile_type = get_compile_type(ufunc);
543 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000544 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000545 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100546 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000547}
548
549/*
550 * Compile a variable name into a load instruction.
551 * "end" points to just after the name.
552 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
553 * When "error" is FALSE do not give an error when not found.
554 */
555 int
556compile_load(
557 char_u **arg,
558 char_u *end_arg,
559 cctx_T *cctx,
560 int is_expr,
561 int error)
562{
563 type_T *type;
564 char_u *name = NULL;
565 char_u *end = end_arg;
566 int res = FAIL;
567 int prev_called_emsg = called_emsg;
568
569 if (*(*arg + 1) == ':')
570 {
571 if (end <= *arg + 2)
572 {
573 isntype_T isn_type;
574
575 // load dictionary of namespace
576 switch (**arg)
577 {
578 case 'g': isn_type = ISN_LOADGDICT; break;
579 case 'w': isn_type = ISN_LOADWDICT; break;
580 case 't': isn_type = ISN_LOADTDICT; break;
581 case 'b': isn_type = ISN_LOADBDICT; break;
582 default:
583 semsg(_(e_namespace_not_supported_str), *arg);
584 goto theend;
585 }
586 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
587 goto theend;
588 res = OK;
589 }
590 else
591 {
592 isntype_T isn_type = ISN_DROP;
593
594 // load namespaced variable
595 name = vim_strnsave(*arg + 2, end - (*arg + 2));
596 if (name == NULL)
597 return FAIL;
598
599 switch (**arg)
600 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100601 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000602 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000603 case 's': if (current_script_is_vim9())
604 {
605 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
606 *arg);
607 vim_free(name);
608 return FAIL;
609 }
Kota Kato948a3892022-08-16 16:09:59 +0100610 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000611 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000612 else
613 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100614 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000615 break;
616 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
617 {
618 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000619 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000620 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000621 else
622 isn_type = ISN_LOADG;
623 }
624 else
625 {
626 isn_type = ISN_LOADAUTO;
627 vim_free(name);
628 name = vim_strnsave(*arg, end - *arg);
629 if (name == NULL)
630 return FAIL;
631 }
632 break;
633 case 'w': isn_type = ISN_LOADW; break;
634 case 't': isn_type = ISN_LOADT; break;
635 case 'b': isn_type = ISN_LOADB; break;
636 default: // cannot happen, just in case
637 semsg(_(e_namespace_not_supported_str), *arg);
638 goto theend;
639 }
640 if (isn_type != ISN_DROP)
641 {
642 // Global, Buffer-local, Window-local and Tabpage-local
643 // variables can be defined later, thus we don't check if it
644 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000645 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000646 }
647 }
648 }
649 else
650 {
651 size_t len = end - *arg;
652 int idx;
653 int gen_load = FALSE;
654 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100655 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100656 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000657
658 name = vim_strnsave(*arg, end - *arg);
659 if (name == NULL)
660 return FAIL;
661
Bram Moolenaar58b40092023-01-11 15:59:05 +0000662 if (STRCMP(name, "super") == 0
663 && cctx->ctx_ufunc != NULL
664 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)) == 0)
665 {
666 // super.SomeFunc() in a class function: push &t_super type, this
667 // is recognized in compile_subscript().
668 res = push_type_stack(cctx, &t_super);
669 if (*end != '.')
670 emsg(_(e_super_must_be_followed_by_dot));
671 }
672 else if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000673 {
674 script_autoload(name, FALSE);
675 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
676 }
677 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
678 == OK)
679 {
680 if (gen_load_outer == 0)
681 gen_load = TRUE;
682 }
683 else
684 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000685 lvar_T lvar;
686 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000687
688 if (lookup_local(*arg, len, &lvar, cctx) == OK)
689 {
690 type = lvar.lv_type;
691 idx = lvar.lv_idx;
692 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100693 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000694 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100695 outer_loop_depth = lvar.lv_loop_depth;
696 outer_loop_idx = lvar.lv_loop_idx;
697 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000698 else
699 gen_load = TRUE;
700 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000701 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000702 {
703 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
704 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000705 else
706 {
707 // "var" can be script-local even without using "s:" if it
708 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000709 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000710 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100711 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000712
713 // When evaluating an expression and the name starts with an
714 // uppercase letter it can be a user defined function.
715 // generate_funcref() will fail if the function can't be found.
716 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000717 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000718 }
719 }
720 if (gen_load)
721 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
722 if (gen_load_outer > 0)
723 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100724 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
725 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000726 cctx->ctx_outer_used = TRUE;
727 }
728 }
729
730 *arg = end;
731
732theend:
733 if (res == FAIL && error && called_emsg == prev_called_emsg)
734 semsg(_(e_variable_not_found_str), name);
735 vim_free(name);
736 return res;
737}
738
739/*
740 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100741 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000742 * Returns FAIL if compilation fails.
743 */
744 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100745compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746{
LemonBoyf3b48952022-05-05 13:53:03 +0100747 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000748 garray_T save_ga = cctx->ctx_instr;
749 int expr_res;
750 int trailing_error;
751 int instr_count;
752 isn_T *instr = NULL;
753
754 // Remove the string type from the stack.
755 --cctx->ctx_type_stack.ga_len;
756
757 // Temporarily reset the list of instructions so that the jump labels are
758 // correct.
759 cctx->ctx_instr.ga_len = 0;
760 cctx->ctx_instr.ga_maxlen = 0;
761 cctx->ctx_instr.ga_data = NULL;
h-east01c5f2a2023-01-09 15:10:40 +0000762
763 // avoid peeking a next line
764 int galen_save = cctx->ctx_ufunc->uf_lines.ga_len;
765 cctx->ctx_ufunc->uf_lines.ga_len = 0;
766
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000767 expr_res = compile_expr0(&s, cctx);
h-east01c5f2a2023-01-09 15:10:40 +0000768
769 cctx->ctx_ufunc->uf_lines.ga_len = galen_save;
770
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000771 s = skipwhite(s);
772 trailing_error = *s != NUL;
773
774 if (expr_res == FAIL || trailing_error
775 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
776 {
777 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000778 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000779 clear_instr_ga(&cctx->ctx_instr);
780 cctx->ctx_instr = save_ga;
781 ++cctx->ctx_type_stack.ga_len;
782 return FAIL;
783 }
784
785 // Move the generated instructions into the ISN_INSTR instruction, then
786 // restore the list of instructions.
787 instr_count = cctx->ctx_instr.ga_len;
788 instr = cctx->ctx_instr.ga_data;
789 instr[instr_count].isn_type = ISN_FINISH;
790
791 cctx->ctx_instr = save_ga;
792 vim_free(isn->isn_arg.string);
793 isn->isn_type = ISN_INSTR;
794 isn->isn_arg.instr = instr;
795 return OK;
796}
797
798/*
799 * Compile the argument expressions.
800 * "arg" points to just after the "(" and is advanced to after the ")"
801 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100802 int
LemonBoyf3b48952022-05-05 13:53:03 +0100803compile_arguments(
804 char_u **arg,
805 cctx_T *cctx,
806 int *argcount,
807 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000808{
809 char_u *p = *arg;
810 char_u *whitep = *arg;
811 int must_end = FALSE;
812 int instr_count;
813
814 for (;;)
815 {
816 if (may_get_next_line(whitep, &p, cctx) == FAIL)
817 goto failret;
818 if (*p == ')')
819 {
820 *arg = p + 1;
821 return OK;
822 }
823 if (must_end)
824 {
825 semsg(_(e_missing_comma_before_argument_str), p);
826 return FAIL;
827 }
828
829 instr_count = cctx->ctx_instr.ga_len;
830 if (compile_expr0(&p, cctx) == FAIL)
831 return FAIL;
832 ++*argcount;
833
LemonBoyf3b48952022-05-05 13:53:03 +0100834 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000835 && cctx->ctx_instr.ga_len == instr_count + 1)
836 {
837 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
838
839 // {skip} argument of searchpair() can be compiled if not empty
840 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100841 compile_string(isn, cctx, 0);
842 }
843 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
844 && cctx->ctx_instr.ga_len == instr_count + 1)
845 {
846 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
847
848 // {sub} argument of substitute() can be compiled if it starts
849 // with \=
850 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100851 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100852 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853 }
854
855 if (*p != ',' && *skipwhite(p) == ',')
856 {
857 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
858 p = skipwhite(p);
859 }
860 if (*p == ',')
861 {
862 ++p;
863 if (*p != NUL && !VIM_ISWHITE(*p))
864 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
865 }
866 else
867 must_end = TRUE;
868 whitep = p;
869 p = skipwhite(p);
870 }
871failret:
872 emsg(_(e_missing_closing_paren));
873 return FAIL;
874}
875
876/*
877 * Compile a function call: name(arg1, arg2)
878 * "arg" points to "name", "arg + varlen" to the "(".
879 * "argcount_init" is 1 for "value->method()"
880 * Instructions:
881 * EVAL arg1
882 * EVAL arg2
883 * BCALL / DCALL / UCALL
884 */
885 static int
886compile_call(
887 char_u **arg,
888 size_t varlen,
889 cctx_T *cctx,
890 ppconst_T *ppconst,
891 int argcount_init)
892{
893 char_u *name = *arg;
894 char_u *p;
895 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100896 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 char_u fname_buf[FLEN_FIXED + 1];
898 char_u *tofree = NULL;
899 int error = FCERR_NONE;
900 ufunc_T *ufunc = NULL;
901 int res = FAIL;
902 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000903 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100904 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000905 imported_T *import;
906
907 if (varlen >= sizeof(namebuf))
908 {
909 semsg(_(e_name_too_long_str), name);
910 return FAIL;
911 }
912 vim_strncpy(namebuf, *arg, varlen);
913
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000914 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000915 if (import != NULL)
916 {
917 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
918 return FAIL;
919 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000920
921 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100922 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000923 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100924 if ((varlen == 3
925 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000926 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
927 {
928 char_u *s = skipwhite(*arg + varlen + 1);
929 typval_T argvars[2];
930 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100931 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000932
933 argvars[0].v_type = VAR_UNKNOWN;
934 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100935 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000936 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100937 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000938 s = skipwhite(s);
939 if (*s == ')' && argvars[0].v_type == VAR_STRING
940 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
941 || !is_has))
942 {
943 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
944
945 *arg = s + 1;
946 argvars[1].v_type = VAR_UNKNOWN;
947 tv->v_type = VAR_NUMBER;
948 tv->vval.v_number = 0;
949 if (is_has)
950 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100951 else if (is_len)
952 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000953 else
954 f_exists(argvars, tv);
955 clear_tv(&argvars[0]);
956 ++ppconst->pp_used;
957 return OK;
958 }
959 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100960 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000961 {
962 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
963 return FAIL;
964 }
965 }
966
967 if (generate_ppconst(cctx, ppconst) == FAIL)
968 return FAIL;
969
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000970 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
971
972 // We handle the "skip" argument of searchpair() and searchpairpos()
973 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100974 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
975 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
976 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
977 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
978 special_fn = CA_SEARCHPAIR;
979 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
980 special_fn = CA_SUBSTITUTE;
981 else
982 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983
984 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100985 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000986 goto theend;
987
988 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
989 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
990 {
991 int idx;
992
993 // builtin function
994 idx = find_internal_func(name);
995 if (idx >= 0)
996 {
997 if (STRCMP(name, "flatten") == 0)
998 {
999 emsg(_(e_cannot_use_flatten_in_vim9_script));
1000 goto theend;
1001 }
1002
1003 if (STRCMP(name, "add") == 0 && argcount == 2)
1004 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +00001005 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001006
1007 // add() can be compiled to instructions if we know the type
1008 if (type->tt_type == VAR_LIST)
1009 {
1010 // inline "add(list, item)" so that the type can be checked
1011 res = generate_LISTAPPEND(cctx);
1012 idx = -1;
1013 }
1014 else if (type->tt_type == VAR_BLOB)
1015 {
1016 // inline "add(blob, nr)" so that the type can be checked
1017 res = generate_BLOBAPPEND(cctx);
1018 idx = -1;
1019 }
1020 }
1021
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001022 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
1023 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +01001024 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001025 // May have the "D" or "R" flag, reserve a variable for a
1026 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +01001027 if (get_defer_var_idx(cctx) == 0)
1028 idx = -1;
1029 }
1030
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001031 if (idx >= 0)
1032 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
1033 }
1034 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001035 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001036 goto theend;
1037 }
1038
Bram Moolenaar62aec932022-01-29 21:45:34 +00001039 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
1040
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001041 // An argument or local variable can be a function reference, this
1042 // overrules a function name.
1043 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
1044 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
1045 {
1046 // If we can find the function by name generate the right call.
1047 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001048 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001049 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001050 {
Bram Moolenaar62aec932022-01-29 21:45:34 +00001051 if (!func_is_global(ufunc))
1052 {
1053 res = generate_CALL(cctx, ufunc, argcount);
1054 goto theend;
1055 }
1056 if (!has_g_namespace
1057 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
1058 {
1059 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001060 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +00001061 goto theend;
1062 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001063 }
1064 }
1065
1066 // If the name is a variable, load it and use PCALL.
1067 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001068 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001069 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +00001070 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001071 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
1072 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001073 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001074
1075 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
1076 goto theend;
1077 }
1078
1079 // If we can find a global function by name generate the right call.
1080 if (ufunc != NULL)
1081 {
1082 res = generate_CALL(cctx, ufunc, argcount);
1083 goto theend;
1084 }
1085
1086 // A global function may be defined only later. Need to figure out at
1087 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +00001088 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001089 res = generate_UCALL(cctx, name, argcount);
1090 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001091 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001092
1093theend:
1094 vim_free(tofree);
1095 return res;
1096}
1097
1098// like NAMESPACE_CHAR but with 'a' and 'l'.
1099#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
1100
1101/*
1102 * Find the end of a variable or function name. Unlike find_name_end() this
1103 * does not recognize magic braces.
1104 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1105 * Return a pointer to just after the name. Equal to "arg" if there is no
1106 * valid name.
1107 */
1108 char_u *
1109to_name_end(char_u *arg, int use_namespace)
1110{
1111 char_u *p;
1112
1113 // Quick check for valid starting character.
1114 if (!eval_isnamec1(*arg))
1115 return arg;
1116
1117 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1118 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1119 // and can be used in slice "[n:]".
1120 if (*p == ':' && (p != arg + 1
1121 || !use_namespace
1122 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1123 break;
1124 return p;
1125}
1126
1127/*
1128 * Like to_name_end() but also skip over a list or dict constant.
1129 * Also accept "<SNR>123_Func".
1130 * This intentionally does not handle line continuation.
1131 */
1132 char_u *
1133to_name_const_end(char_u *arg)
1134{
1135 char_u *p = arg;
1136 typval_T rettv;
1137
1138 if (STRNCMP(p, "<SNR>", 5) == 0)
1139 p = skipdigits(p + 5);
1140 p = to_name_end(p, TRUE);
1141 if (p == arg && *arg == '[')
1142 {
1143
1144 // Can be "[1, 2, 3]->Func()".
1145 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1146 p = arg;
1147 }
1148 return p;
1149}
1150
1151/*
1152 * parse a list: [expr, expr]
1153 * "*arg" points to the '['.
1154 * ppconst->pp_is_const is set if all items are a constant.
1155 */
1156 static int
1157compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1158{
1159 char_u *p = skipwhite(*arg + 1);
1160 char_u *whitep = *arg + 1;
1161 int count = 0;
1162 int is_const;
1163 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001164 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001165
1166 for (;;)
1167 {
1168 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1169 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001170 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001171 return FAIL;
1172 }
1173 if (*p == ',')
1174 {
1175 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1176 return FAIL;
1177 }
1178 if (*p == ']')
1179 {
1180 ++p;
1181 break;
1182 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001183 if (must_end)
1184 {
1185 semsg(_(e_missing_comma_in_list_str), p);
1186 return FAIL;
1187 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001188 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1189 return FAIL;
1190 if (!is_const)
1191 is_all_const = FALSE;
1192 ++count;
1193 if (*p == ',')
1194 {
1195 ++p;
1196 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1197 {
1198 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1199 return FAIL;
1200 }
1201 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001202 else
1203 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001204 whitep = p;
1205 p = skipwhite(p);
1206 }
1207 *arg = p;
1208
1209 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001210 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001211}
1212
1213/*
1214 * Parse a lambda: "(arg, arg) => expr"
1215 * "*arg" points to the '('.
1216 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1217 */
1218 static int
1219compile_lambda(char_u **arg, cctx_T *cctx)
1220{
1221 int r;
1222 typval_T rettv;
1223 ufunc_T *ufunc;
1224 evalarg_T evalarg;
1225
1226 init_evalarg(&evalarg);
1227 evalarg.eval_flags = EVAL_EVALUATE;
1228 evalarg.eval_cctx = cctx;
1229
1230 // Get the funcref in "rettv".
1231 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1232 if (r != OK)
1233 {
1234 clear_evalarg(&evalarg, NULL);
1235 return r;
1236 }
1237
1238 // "rettv" will now be a partial referencing the function.
1239 ufunc = rettv.vval.v_partial->pt_func;
1240 ++ufunc->uf_refcount;
1241 clear_tv(&rettv);
1242
1243 // Compile it here to get the return type. The return type is optional,
1244 // when it's missing use t_unknown. This is recognized in
1245 // compile_return().
1246 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1247 ufunc->uf_ret_type = &t_unknown;
1248 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1249
1250 // When the outer function is compiled for profiling or debugging, the
1251 // lambda may be called without profiling or debugging. Compile it here in
1252 // the right context.
1253 if (cctx->ctx_compile_type == CT_DEBUG
1254#ifdef FEAT_PROFILE
1255 || cctx->ctx_compile_type == CT_PROFILE
1256#endif
1257 )
1258 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1259
Bram Moolenaar139575d2022-03-15 19:29:30 +00001260 // if the outer function is not compiled for debugging or profiling, this
1261 // one might be
1262 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001263 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001264 compiletype_T compile_type = get_compile_type(ufunc);
1265
1266 if (compile_type != CT_NONE)
1267 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001268 }
1269
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001270 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1271 // "*arg" may point into it. Point into the original line to avoid a
1272 // dangling pointer.
1273 if (evalarg.eval_using_cmdline)
1274 {
1275 garray_T *gap = &evalarg.eval_tofree_ga;
1276 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1277
1278 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1279 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001280 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001281 }
1282
1283 clear_evalarg(&evalarg, NULL);
1284
1285 if (ufunc->uf_def_status == UF_COMPILED)
1286 {
1287 // The return type will now be known.
1288 set_function_type(ufunc);
1289
1290 // The function reference count will be 1. When the ISN_FUNCREF
1291 // instruction is deleted the reference count is decremented and the
1292 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001293 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001294 }
1295
1296 func_ptr_unref(ufunc);
1297 return FAIL;
1298}
1299
1300/*
1301 * Get a lambda and compile it. Uses Vim9 syntax.
1302 */
1303 int
1304get_lambda_tv_and_compile(
1305 char_u **arg,
1306 typval_T *rettv,
1307 int types_optional,
1308 evalarg_T *evalarg)
1309{
1310 int r;
1311 ufunc_T *ufunc;
1312 int save_sc_version = current_sctx.sc_version;
1313
1314 // Get the funcref in "rettv".
1315 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1316 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1317 current_sctx.sc_version = save_sc_version;
1318 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001319 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001320
1321 // "rettv" will now be a partial referencing the function.
1322 ufunc = rettv->vval.v_partial->pt_func;
1323
1324 // Compile it here to get the return type. The return type is optional,
1325 // when it's missing use t_unknown. This is recognized in
1326 // compile_return().
1327 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1328 ufunc->uf_ret_type = &t_unknown;
1329 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1330
1331 if (ufunc->uf_def_status == UF_COMPILED)
1332 {
1333 // The return type will now be known.
1334 set_function_type(ufunc);
1335 return OK;
1336 }
1337 clear_tv(rettv);
1338 return FAIL;
1339}
1340
1341/*
1342 * parse a dict: {key: val, [key]: val}
1343 * "*arg" points to the '{'.
1344 * ppconst->pp_is_const is set if all item values are a constant.
1345 */
1346 static int
1347compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1348{
1349 garray_T *instr = &cctx->ctx_instr;
1350 int count = 0;
1351 dict_T *d = dict_alloc();
1352 dictitem_T *item;
1353 char_u *whitep = *arg + 1;
1354 char_u *p;
1355 int is_const;
1356 int is_all_const = TRUE; // reset when non-const encountered
1357
1358 if (d == NULL)
1359 return FAIL;
1360 if (generate_ppconst(cctx, ppconst) == FAIL)
1361 return FAIL;
1362 for (;;)
1363 {
1364 char_u *key = NULL;
1365
1366 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1367 {
1368 *arg = NULL;
1369 goto failret;
1370 }
1371
1372 if (**arg == '}')
1373 break;
1374
1375 if (**arg == '[')
1376 {
1377 isn_T *isn;
1378
1379 // {[expr]: value} uses an evaluated key.
1380 *arg = skipwhite(*arg + 1);
1381 if (compile_expr0(arg, cctx) == FAIL)
1382 return FAIL;
1383 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1384 if (isn->isn_type == ISN_PUSHNR)
1385 {
1386 char buf[NUMBUFLEN];
1387
1388 // Convert to string at compile time.
1389 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1390 isn->isn_type = ISN_PUSHS;
1391 isn->isn_arg.string = vim_strsave((char_u *)buf);
1392 }
1393 if (isn->isn_type == ISN_PUSHS)
1394 key = isn->isn_arg.string;
1395 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1396 return FAIL;
1397 *arg = skipwhite(*arg);
1398 if (**arg != ']')
1399 {
1400 emsg(_(e_missing_matching_bracket_after_dict_key));
1401 return FAIL;
1402 }
1403 ++*arg;
1404 }
1405 else
1406 {
1407 // {"name": value},
1408 // {'name': value},
1409 // {name: value} use "name" as a literal key
1410 key = get_literal_key(arg);
1411 if (key == NULL)
1412 return FAIL;
1413 if (generate_PUSHS(cctx, &key) == FAIL)
1414 return FAIL;
1415 }
1416
1417 // Check for duplicate keys, if using string keys.
1418 if (key != NULL)
1419 {
1420 item = dict_find(d, key, -1);
1421 if (item != NULL)
1422 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001423 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001424 goto failret;
1425 }
1426 item = dictitem_alloc(key);
1427 if (item != NULL)
1428 {
1429 item->di_tv.v_type = VAR_UNKNOWN;
1430 item->di_tv.v_lock = 0;
1431 if (dict_add(d, item) == FAIL)
1432 dictitem_free(item);
1433 }
1434 }
1435
1436 if (**arg != ':')
1437 {
1438 if (*skipwhite(*arg) == ':')
1439 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1440 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001441 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001442 return FAIL;
1443 }
1444 whitep = *arg + 1;
1445 if (!IS_WHITE_OR_NUL(*whitep))
1446 {
1447 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1448 return FAIL;
1449 }
1450
1451 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1452 {
1453 *arg = NULL;
1454 goto failret;
1455 }
1456
1457 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1458 return FAIL;
1459 if (!is_const)
1460 is_all_const = FALSE;
1461 ++count;
1462
1463 whitep = *arg;
1464 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1465 {
1466 *arg = NULL;
1467 goto failret;
1468 }
1469 if (**arg == '}')
1470 break;
1471 if (**arg != ',')
1472 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001473 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001474 goto failret;
1475 }
1476 if (IS_WHITE_OR_NUL(*whitep))
1477 {
1478 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1479 return FAIL;
1480 }
1481 whitep = *arg + 1;
1482 if (!IS_WHITE_OR_NUL(*whitep))
1483 {
1484 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1485 return FAIL;
1486 }
1487 *arg = skipwhite(whitep);
1488 }
1489
1490 *arg = *arg + 1;
1491
1492 // Allow for following comment, after at least one space.
1493 p = skipwhite(*arg);
1494 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1495 *arg += STRLEN(*arg);
1496
1497 dict_unref(d);
1498 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001499 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001500
1501failret:
1502 if (*arg == NULL)
1503 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001504 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001505 *arg = (char_u *)"";
1506 }
1507 dict_unref(d);
1508 return FAIL;
1509}
1510
1511/*
1512 * Compile "&option".
1513 */
1514 static int
1515compile_get_option(char_u **arg, cctx_T *cctx)
1516{
1517 typval_T rettv;
1518 char_u *start = *arg;
1519 int ret;
1520
1521 // parse the option and get the current value to get the type.
1522 rettv.v_type = VAR_UNKNOWN;
1523 ret = eval_option(arg, &rettv, TRUE);
1524 if (ret == OK)
1525 {
1526 // include the '&' in the name, eval_option() expects it.
1527 char_u *name = vim_strnsave(start, *arg - start);
1528 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1529 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1530
1531 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1532 vim_free(name);
1533 }
1534 clear_tv(&rettv);
1535
1536 return ret;
1537}
1538
1539/*
1540 * Compile "$VAR".
1541 */
1542 static int
1543compile_get_env(char_u **arg, cctx_T *cctx)
1544{
1545 char_u *start = *arg;
1546 int len;
1547 int ret;
1548 char_u *name;
1549
1550 ++*arg;
1551 len = get_env_len(arg);
1552 if (len == 0)
1553 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001554 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001555 return FAIL;
1556 }
1557
1558 // include the '$' in the name, eval_env_var() expects it.
1559 name = vim_strnsave(start, len + 1);
1560 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1561 vim_free(name);
1562 return ret;
1563}
1564
1565/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001566 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001567 */
1568 static int
1569compile_interp_string(char_u **arg, cctx_T *cctx)
1570{
1571 typval_T tv;
1572 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001573 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001574 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001575 int count = 0;
1576 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001577
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001578 // *arg is on the '$' character, move it to the first string character.
1579 ++*arg;
1580 quote = **arg;
1581 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001582
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001583 for (;;)
1584 {
1585 // Get the string up to the matching quote or to a single '{'.
1586 // "arg" is advanced to either the quote or the '{'.
1587 if (quote == '"')
1588 ret = eval_string(arg, &tv, evaluate, TRUE);
1589 else
1590 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1591 if (ret == FAIL)
1592 break;
1593 if (evaluate)
1594 {
1595 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1596 || (**arg != '{' && count == 0))
1597 {
1598 // generate non-empty string or empty string if it's the only
1599 // one
1600 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1601 return FAIL;
1602 tv.vval.v_string = NULL; // don't free it now
1603 ++count;
1604 }
1605 clear_tv(&tv);
1606 }
1607
1608 if (**arg != '{')
1609 {
1610 // found terminating quote
1611 ++*arg;
1612 break;
1613 }
1614
1615 p = compile_one_expr_in_str(*arg, cctx);
1616 if (p == NULL)
1617 {
1618 ret = FAIL;
1619 break;
1620 }
1621 ++count;
1622 *arg = p;
1623 }
LemonBoy2eaef102022-05-06 13:14:50 +01001624
1625 if (ret == FAIL || !evaluate)
1626 return ret;
1627
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001628 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1629 if (count > 1)
1630 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001631
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001632 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001633}
1634
1635/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001636 * Compile "@r".
1637 */
1638 static int
1639compile_get_register(char_u **arg, cctx_T *cctx)
1640{
1641 int ret;
1642
1643 ++*arg;
1644 if (**arg == NUL)
1645 {
1646 semsg(_(e_syntax_error_at_str), *arg - 1);
1647 return FAIL;
1648 }
1649 if (!valid_yank_reg(**arg, FALSE))
1650 {
1651 emsg_invreg(**arg);
1652 return FAIL;
1653 }
1654 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1655 ++*arg;
1656 return ret;
1657}
1658
1659/*
1660 * Apply leading '!', '-' and '+' to constant "rettv".
1661 * When "numeric_only" is TRUE do not apply '!'.
1662 */
1663 static int
1664apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1665{
1666 char_u *p = *end;
1667
1668 // this works from end to start
1669 while (p > start)
1670 {
1671 --p;
1672 if (*p == '-' || *p == '+')
1673 {
1674 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001675 if (rettv->v_type == VAR_FLOAT)
1676 {
1677 if (*p == '-')
1678 rettv->vval.v_float = -rettv->vval.v_float;
1679 }
1680 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001681 {
1682 varnumber_T val;
1683 int error = FALSE;
1684
1685 // tv_get_number_chk() accepts a string, but we don't want that
1686 // here
1687 if (check_not_string(rettv) == FAIL)
1688 return FAIL;
1689 val = tv_get_number_chk(rettv, &error);
1690 clear_tv(rettv);
1691 if (error)
1692 return FAIL;
1693 if (*p == '-')
1694 val = -val;
1695 rettv->v_type = VAR_NUMBER;
1696 rettv->vval.v_number = val;
1697 }
1698 }
1699 else if (numeric_only)
1700 {
1701 ++p;
1702 break;
1703 }
1704 else if (*p == '!')
1705 {
1706 int v = tv2bool(rettv);
1707
1708 // '!' is permissive in the type.
1709 clear_tv(rettv);
1710 rettv->v_type = VAR_BOOL;
1711 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1712 }
1713 }
1714 *end = p;
1715 return OK;
1716}
1717
1718/*
1719 * Recognize v: variables that are constants and set "rettv".
1720 */
1721 static void
1722get_vim_constant(char_u **arg, typval_T *rettv)
1723{
1724 if (STRNCMP(*arg, "v:true", 6) == 0)
1725 {
1726 rettv->v_type = VAR_BOOL;
1727 rettv->vval.v_number = VVAL_TRUE;
1728 *arg += 6;
1729 }
1730 else if (STRNCMP(*arg, "v:false", 7) == 0)
1731 {
1732 rettv->v_type = VAR_BOOL;
1733 rettv->vval.v_number = VVAL_FALSE;
1734 *arg += 7;
1735 }
1736 else if (STRNCMP(*arg, "v:null", 6) == 0)
1737 {
1738 rettv->v_type = VAR_SPECIAL;
1739 rettv->vval.v_number = VVAL_NULL;
1740 *arg += 6;
1741 }
1742 else if (STRNCMP(*arg, "v:none", 6) == 0)
1743 {
1744 rettv->v_type = VAR_SPECIAL;
1745 rettv->vval.v_number = VVAL_NONE;
1746 *arg += 6;
1747 }
1748}
1749
1750 exprtype_T
1751get_compare_type(char_u *p, int *len, int *type_is)
1752{
1753 exprtype_T type = EXPR_UNKNOWN;
1754 int i;
1755
1756 switch (p[0])
1757 {
1758 case '=': if (p[1] == '=')
1759 type = EXPR_EQUAL;
1760 else if (p[1] == '~')
1761 type = EXPR_MATCH;
1762 break;
1763 case '!': if (p[1] == '=')
1764 type = EXPR_NEQUAL;
1765 else if (p[1] == '~')
1766 type = EXPR_NOMATCH;
1767 break;
1768 case '>': if (p[1] != '=')
1769 {
1770 type = EXPR_GREATER;
1771 *len = 1;
1772 }
1773 else
1774 type = EXPR_GEQUAL;
1775 break;
1776 case '<': if (p[1] != '=')
1777 {
1778 type = EXPR_SMALLER;
1779 *len = 1;
1780 }
1781 else
1782 type = EXPR_SEQUAL;
1783 break;
1784 case 'i': if (p[1] == 's')
1785 {
1786 // "is" and "isnot"; but not a prefix of a name
1787 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1788 *len = 5;
1789 i = p[*len];
1790 if (!isalnum(i) && i != '_')
1791 {
1792 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1793 *type_is = TRUE;
1794 }
1795 }
1796 break;
1797 }
1798 return type;
1799}
1800
1801/*
1802 * Skip over an expression, ignoring most errors.
1803 */
1804 void
1805skip_expr_cctx(char_u **arg, cctx_T *cctx)
1806{
1807 evalarg_T evalarg;
1808
1809 init_evalarg(&evalarg);
1810 evalarg.eval_cctx = cctx;
1811 skip_expr(arg, &evalarg);
1812 clear_evalarg(&evalarg, NULL);
1813}
1814
1815/*
1816 * Check that the top of the type stack has a type that can be used as a
1817 * condition. Give an error and return FAIL if not.
1818 */
1819 int
1820bool_on_stack(cctx_T *cctx)
1821{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001822 type_T *type;
1823
Bram Moolenaar078a4612022-01-04 15:17:03 +00001824 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001825 if (type == &t_bool)
1826 return OK;
1827
Bram Moolenaar4913d422022-10-17 13:13:32 +01001828 if (type->tt_type == VAR_ANY
1829 || type->tt_type == VAR_UNKNOWN
1830 || type->tt_type == VAR_NUMBER
1831 || type == &t_number_bool
1832 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001833 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1834 // This requires a runtime type check.
1835 return generate_COND2BOOL(cctx);
1836
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001837 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001838}
1839
1840/*
1841 * Give the "white on both sides" error, taking the operator from "p[len]".
1842 */
1843 void
1844error_white_both(char_u *op, int len)
1845{
1846 char_u buf[10];
1847
1848 vim_strncpy(buf, op, len);
1849 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1850}
1851
1852/*
1853 * Compile code to apply '-', '+' and '!'.
1854 * When "numeric_only" is TRUE do not apply '!'.
1855 */
1856 static int
1857compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1858{
1859 char_u *p = *end;
1860
1861 // this works from end to start
1862 while (p > start)
1863 {
1864 --p;
1865 while (VIM_ISWHITE(*p))
1866 --p;
1867 if (*p == '-' || *p == '+')
1868 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001869 type_T *type = get_type_on_stack(cctx, 0);
1870 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001871 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001872 return FAIL;
1873
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001874 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001875 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1876 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001877 }
1878 else if (numeric_only)
1879 {
1880 ++p;
1881 break;
1882 }
1883 else
1884 {
1885 int invert = *p == '!';
1886
1887 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1888 {
1889 if (p[-1] == '!')
1890 invert = !invert;
1891 --p;
1892 }
1893 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1894 return FAIL;
1895 }
1896 }
1897 *end = p;
1898 return OK;
1899}
1900
1901/*
1902 * Compile "(expression)": recursive!
1903 * Return FAIL/OK.
1904 */
1905 static int
1906compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1907{
1908 int ret;
1909 char_u *p = *arg + 1;
1910
1911 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1912 return FAIL;
1913 if (ppconst->pp_used <= PPSIZE - 10)
1914 {
1915 ret = compile_expr1(arg, cctx, ppconst);
1916 }
1917 else
1918 {
1919 // Not enough space in ppconst, flush constants.
1920 if (generate_ppconst(cctx, ppconst) == FAIL)
1921 return FAIL;
1922 ret = compile_expr0(arg, cctx);
1923 }
1924 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1925 return FAIL;
1926 if (**arg == ')')
1927 ++*arg;
1928 else if (ret == OK)
1929 {
1930 emsg(_(e_missing_closing_paren));
1931 ret = FAIL;
1932 }
1933 return ret;
1934}
1935
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001936static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001937
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001938/*
1939 * Compile whatever comes after "name" or "name()".
1940 * Advances "*arg" only when something was recognized.
1941 */
1942 static int
1943compile_subscript(
1944 char_u **arg,
1945 cctx_T *cctx,
1946 char_u *start_leader,
1947 char_u **end_leader,
1948 ppconst_T *ppconst)
1949{
1950 char_u *name_start = *end_leader;
1951 int keeping_dict = FALSE;
1952
1953 for (;;)
1954 {
1955 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001956 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001957
1958 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1959 {
1960 char_u *next = peek_next_line_from_context(cctx);
1961
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001962 // If a following line starts with "->{", "->(" or "->X" advance to
1963 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001964 // Also if a following line starts with ".x".
1965 if (next != NULL &&
1966 ((next[0] == '-' && next[1] == '>'
1967 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001968 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001969 || ASCII_ISALPHA(*skipwhite(next + 2))))
1970 || (next[0] == '.' && eval_isdictc(next[1]))))
1971 {
1972 next = next_line_from_context(cctx, TRUE);
1973 if (next == NULL)
1974 return FAIL;
1975 *arg = next;
1976 p = skipwhite(*arg);
1977 }
1978 }
1979
1980 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1981 // is not a function call.
1982 if (**arg == '(')
1983 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001984 int argcount = 0;
1985
1986 if (generate_ppconst(cctx, ppconst) == FAIL)
1987 return FAIL;
1988 ppconst->pp_is_const = FALSE;
1989
1990 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001991 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001992
1993 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001994 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001995 return FAIL;
1996 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1997 return FAIL;
1998 if (keeping_dict)
1999 {
2000 keeping_dict = FALSE;
2001 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2002 return FAIL;
2003 }
2004 }
2005 else if (*p == '-' && p[1] == '>')
2006 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00002007 char_u *pstart = p;
2008 int alt;
2009 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002010
Bram Moolenaarc7349932022-01-16 20:59:39 +00002011 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002012 if (generate_ppconst(cctx, ppconst) == FAIL)
2013 return FAIL;
2014 ppconst->pp_is_const = FALSE;
2015
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002016 // Apply the '!', '-' and '+' first:
2017 // -1.0->func() works like (-1.0)->func()
2018 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
2019 return FAIL;
2020
2021 p += 2;
2022 *arg = skipwhite(p);
2023 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00002024
2025 // Three alternatives handled here:
2026 // 1. "base->name(" only a name, use compile_call()
2027 // 2. "base->(expr)(" evaluate "expr", then use PCALL
2028 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002029 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00002030 alt = 2;
2031 else
2032 {
2033 // alternative 1 or 3
2034 p = *arg;
2035 if (!eval_isnamec1(*p))
2036 {
2037 semsg(_(e_trailing_characters_str), pstart);
2038 return FAIL;
2039 }
2040 if (ASCII_ISALPHA(*p) && p[1] == ':')
2041 p += 2;
2042 for ( ; eval_isnamec(*p); ++p)
2043 ;
2044 if (*p == '(')
2045 {
2046 // alternative 1
2047 alt = 1;
2048 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
2049 return FAIL;
2050 }
2051 else
2052 {
2053 // Must be alternative 3, find the "(". Only works within
2054 // one line.
2055 alt = 3;
2056 paren = vim_strchr(p, '(');
2057 if (paren == NULL)
2058 {
2059 semsg(_(e_missing_parenthesis_str), *arg);
2060 return FAIL;
2061 }
2062 }
2063 }
2064
2065 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002066 {
2067 int argcount = 1;
2068 garray_T *stack = &cctx->ctx_type_stack;
2069 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002070 int expr_isn_start = cctx->ctx_instr.ga_len;
2071 int expr_isn_end;
2072 int arg_isn_count;
2073
Bram Moolenaarc7349932022-01-16 20:59:39 +00002074 if (alt == 2)
2075 {
2076 // Funcref call: list->(Refs[2])(arg)
2077 // or lambda: list->((arg) => expr)(arg)
2078 //
2079 // Fist compile the function expression.
2080 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
2081 return FAIL;
2082 }
2083 else
2084 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002085 int fail;
2086 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002087 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002088
Bram Moolenaarc7349932022-01-16 20:59:39 +00002089 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002090
2091 // instead of using LOADG for "import.Func" use PUSHFUNC
2092 ++paren_follows_after_expr;
2093
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002094 // do not look in the next line
2095 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002096
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002097 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002098 || *skipwhite(*arg) != NUL;
2099 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00002100 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002101 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002102
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002103 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002104 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002105 if (did_emsg == prev_did_emsg)
2106 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002107 return FAIL;
2108 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002109 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002110
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002111 // Compile the arguments.
2112 if (**arg != '(')
2113 {
2114 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002115 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002116 else
2117 semsg(_(e_missing_parenthesis_str), *arg);
2118 return FAIL;
2119 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002120
2121 // Remember the next instruction index, where the instructions
2122 // for arguments are being written.
2123 expr_isn_end = cctx->ctx_instr.ga_len;
2124
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002125 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002126 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2127 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002128 return FAIL;
2129
2130 // Move the instructions for the arguments to before the
2131 // instructions of the expression and move the type of the
2132 // expression after the argument types. This is what ISN_PCALL
2133 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002134 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2135 if (arg_isn_count > 0)
2136 {
2137 int expr_isn_count = expr_isn_end - expr_isn_start;
2138 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002139 type_T *decl_type;
2140 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002141
2142 if (isn == NULL)
2143 return FAIL;
2144 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2145 + expr_isn_start,
2146 sizeof(isn_T) * expr_isn_count);
2147 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2148 + expr_isn_start,
2149 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2150 sizeof(isn_T) * arg_isn_count);
2151 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2152 + expr_isn_start + arg_isn_count,
2153 isn, sizeof(isn_T) * expr_isn_count);
2154 vim_free(isn);
2155
Bram Moolenaar078a4612022-01-04 15:17:03 +00002156 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2157 type = typep->type_curr;
2158 decl_type = typep->type_decl;
2159 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2160 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2161 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002162 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002163 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2164 typep->type_curr = type;
2165 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002166 }
2167
Bram Moolenaar078a4612022-01-04 15:17:03 +00002168 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002169 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2170 return FAIL;
2171 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002172
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002173 if (keeping_dict)
2174 {
2175 keeping_dict = FALSE;
2176 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2177 return FAIL;
2178 }
2179 }
2180 else if (**arg == '[')
2181 {
2182 int is_slice = FALSE;
2183
2184 // list index: list[123]
2185 // dict member: dict[key]
2186 // string index: text[123]
2187 // blob index: blob[123]
2188 if (generate_ppconst(cctx, ppconst) == FAIL)
2189 return FAIL;
2190 ppconst->pp_is_const = FALSE;
2191
2192 ++p;
2193 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2194 return FAIL;
2195 if (**arg == ':')
2196 {
2197 // missing first index is equal to zero
2198 generate_PUSHNR(cctx, 0);
2199 }
2200 else
2201 {
2202 if (compile_expr0(arg, cctx) == FAIL)
2203 return FAIL;
2204 if (**arg == ':')
2205 {
2206 semsg(_(e_white_space_required_before_and_after_str_at_str),
2207 ":", *arg);
2208 return FAIL;
2209 }
2210 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2211 return FAIL;
2212 *arg = skipwhite(*arg);
2213 }
2214 if (**arg == ':')
2215 {
2216 is_slice = TRUE;
2217 ++*arg;
2218 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2219 {
2220 semsg(_(e_white_space_required_before_and_after_str_at_str),
2221 ":", *arg);
2222 return FAIL;
2223 }
2224 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2225 return FAIL;
2226 if (**arg == ']')
2227 // missing second index is equal to end of string
2228 generate_PUSHNR(cctx, -1);
2229 else
2230 {
2231 if (compile_expr0(arg, cctx) == FAIL)
2232 return FAIL;
2233 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2234 return FAIL;
2235 *arg = skipwhite(*arg);
2236 }
2237 }
2238
2239 if (**arg != ']')
2240 {
2241 emsg(_(e_missing_closing_square_brace));
2242 return FAIL;
2243 }
2244 *arg = *arg + 1;
2245
2246 if (keeping_dict)
2247 {
2248 keeping_dict = FALSE;
2249 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2250 return FAIL;
2251 }
2252 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2253 return FAIL;
2254 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002255 else if (*p == '.'
2256 && (type = get_type_on_stack(cctx, 0)) != &t_unknown
2257 && (type->tt_type == VAR_CLASS || type->tt_type == VAR_OBJECT))
2258 {
2259 // class member: SomeClass.varname
2260 // class method: SomeClass.SomeMethod()
2261 // class constructor: SomeClass.new()
2262 // object member: someObject.varname, this.varname
2263 // object method: someObject.SomeMethod(), this.SomeMethod()
2264 if (compile_class_object_index(cctx, arg, type) == FAIL)
2265 return FAIL;
2266 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002267 else if (*p == '.' && p[1] != '.')
2268 {
2269 // dictionary member: dict.name
2270 if (generate_ppconst(cctx, ppconst) == FAIL)
2271 return FAIL;
2272 ppconst->pp_is_const = FALSE;
2273
2274 *arg = p + 1;
2275 if (IS_WHITE_OR_NUL(**arg))
2276 {
2277 emsg(_(e_missing_name_after_dot));
2278 return FAIL;
2279 }
2280 p = *arg;
2281 if (eval_isdictc(*p))
2282 while (eval_isnamec(*p))
2283 MB_PTR_ADV(p);
2284 if (p == *arg)
2285 {
2286 semsg(_(e_syntax_error_at_str), *arg);
2287 return FAIL;
2288 }
2289 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2290 return FAIL;
2291 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2292 return FAIL;
2293 keeping_dict = TRUE;
2294 *arg = p;
2295 }
2296 else
2297 break;
2298 }
2299
2300 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2301 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002302 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2303 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002304 return FAIL;
2305
2306 return OK;
2307}
2308
2309/*
2310 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2311 * "arg" is advanced until after the expression, skipping white space.
2312 *
2313 * If the value is a constant "ppconst->pp_used" will be non-zero.
2314 * Before instructions are generated, any values in "ppconst" will generated.
2315 *
2316 * This is the compiling equivalent of eval1(), eval2(), etc.
2317 */
2318
2319/*
2320 * number number constant
2321 * 0zFFFFFFFF Blob constant
2322 * "string" string constant
2323 * 'string' literal string constant
2324 * &option-name option value
2325 * @r register contents
2326 * identifier variable value
2327 * function() function call
2328 * $VAR environment variable
2329 * (expression) nested expression
2330 * [expr, expr] List
2331 * {key: val, [key]: val} Dictionary
2332 *
2333 * Also handle:
2334 * ! in front logical NOT
2335 * - in front unary minus
2336 * + in front unary plus (ignored)
2337 * trailing (arg) funcref/partial call
2338 * trailing [] subscript in String or List
2339 * trailing .name entry in Dictionary
2340 * trailing ->name() method call
2341 */
2342 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002343compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002344 char_u **arg,
2345 cctx_T *cctx,
2346 ppconst_T *ppconst)
2347{
2348 char_u *start_leader, *end_leader;
2349 int ret = OK;
2350 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2351 int used_before = ppconst->pp_used;
2352
2353 ppconst->pp_is_const = FALSE;
2354
2355 /*
2356 * Skip '!', '-' and '+' characters. They are handled later.
2357 */
2358 start_leader = *arg;
2359 if (eval_leader(arg, TRUE) == FAIL)
2360 return FAIL;
2361 end_leader = *arg;
2362
2363 rettv->v_type = VAR_UNKNOWN;
2364 switch (**arg)
2365 {
2366 /*
2367 * Number constant.
2368 */
2369 case '0': // also for blob starting with 0z
2370 case '1':
2371 case '2':
2372 case '3':
2373 case '4':
2374 case '5':
2375 case '6':
2376 case '7':
2377 case '8':
2378 case '9':
2379 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2380 return FAIL;
2381 // Apply "-" and "+" just before the number now, right to
2382 // left. Matters especially when "->" follows. Stops at
2383 // '!'.
2384 if (apply_leader(rettv, TRUE,
2385 start_leader, &end_leader) == FAIL)
2386 {
2387 clear_tv(rettv);
2388 return FAIL;
2389 }
2390 break;
2391
2392 /*
2393 * String constant: "string".
2394 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002395 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002396 return FAIL;
2397 break;
2398
2399 /*
2400 * Literal string constant: 'str''ing'.
2401 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002402 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002403 return FAIL;
2404 break;
2405
2406 /*
2407 * Constant Vim variable.
2408 */
2409 case 'v': get_vim_constant(arg, rettv);
2410 ret = NOTDONE;
2411 break;
2412
2413 /*
2414 * "true" constant
2415 */
2416 case 't': if (STRNCMP(*arg, "true", 4) == 0
2417 && !eval_isnamec((*arg)[4]))
2418 {
2419 *arg += 4;
2420 rettv->v_type = VAR_BOOL;
2421 rettv->vval.v_number = VVAL_TRUE;
2422 }
2423 else
2424 ret = NOTDONE;
2425 break;
2426
2427 /*
2428 * "false" constant
2429 */
2430 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2431 && !eval_isnamec((*arg)[5]))
2432 {
2433 *arg += 5;
2434 rettv->v_type = VAR_BOOL;
2435 rettv->vval.v_number = VVAL_FALSE;
2436 }
2437 else
2438 ret = NOTDONE;
2439 break;
2440
2441 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002442 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002443 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002444 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002445 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002446 char_u *p = *arg + 4;
2447 int len;
2448
2449 for (len = 0; eval_isnamec(p[len]); ++len)
2450 ;
2451 ret = handle_predefined(*arg, len + 4, rettv);
2452 if (ret == FAIL)
2453 ret = NOTDONE;
2454 else
2455 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002456 }
2457 else
2458 ret = NOTDONE;
2459 break;
2460
2461 /*
2462 * List: [expr, expr]
2463 */
2464 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2465 return FAIL;
2466 ret = compile_list(arg, cctx, ppconst);
2467 break;
2468
2469 /*
2470 * Dictionary: {'key': val, 'key': val}
2471 */
2472 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2473 return FAIL;
2474 ret = compile_dict(arg, cctx, ppconst);
2475 break;
2476
2477 /*
2478 * Option value: &name
2479 */
2480 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2481 return FAIL;
2482 ret = compile_get_option(arg, cctx);
2483 break;
2484
2485 /*
2486 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002487 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002488 */
2489 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2490 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002491 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2492 ret = compile_interp_string(arg, cctx);
2493 else
2494 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002495 break;
2496
2497 /*
2498 * Register contents: @r.
2499 */
2500 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2501 return FAIL;
2502 ret = compile_get_register(arg, cctx);
2503 break;
2504 /*
2505 * nested expression: (expression).
2506 * lambda: (arg, arg) => expr
2507 * funcref: (arg, arg) => { statement }
2508 */
2509 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2510 ret = compile_lambda(arg, cctx);
2511 if (ret == NOTDONE)
2512 ret = compile_parenthesis(arg, cctx, ppconst);
2513 break;
2514
2515 default: ret = NOTDONE;
2516 break;
2517 }
2518 if (ret == FAIL)
2519 return FAIL;
2520
2521 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2522 {
2523 if (cctx->ctx_skip == SKIP_YES)
2524 clear_tv(rettv);
2525 else
2526 // A constant expression can possibly be handled compile time,
2527 // return the value instead of generating code.
2528 ++ppconst->pp_used;
2529 }
2530 else if (ret == NOTDONE)
2531 {
2532 char_u *p;
2533 int r;
2534
2535 if (!eval_isnamec1(**arg))
2536 {
2537 if (!vim9_bad_comment(*arg))
2538 {
2539 if (ends_excmd(*skipwhite(*arg)))
2540 semsg(_(e_empty_expression_str), *arg);
2541 else
2542 semsg(_(e_name_expected_str), *arg);
2543 }
2544 return FAIL;
2545 }
2546
2547 // "name" or "name()"
2548 p = to_name_end(*arg, TRUE);
2549 if (p - *arg == (size_t)1 && **arg == '_')
2550 {
2551 emsg(_(e_cannot_use_underscore_here));
2552 return FAIL;
2553 }
2554
2555 if (*p == '(')
2556 {
2557 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2558 }
2559 else
2560 {
2561 if (cctx->ctx_skip != SKIP_YES
2562 && generate_ppconst(cctx, ppconst) == FAIL)
2563 return FAIL;
2564 r = compile_load(arg, p, cctx, TRUE, TRUE);
2565 }
2566 if (r == FAIL)
2567 return FAIL;
2568 }
2569
2570 // Handle following "[]", ".member", etc.
2571 // Then deal with prefixed '-', '+' and '!', if not done already.
2572 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2573 ppconst) == FAIL)
2574 return FAIL;
2575 if (ppconst->pp_used > 0)
2576 {
2577 // apply the '!', '-' and '+' before the constant
2578 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2579 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2580 return FAIL;
2581 return OK;
2582 }
2583 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2584 return FAIL;
2585 return OK;
2586}
2587
2588/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002589 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002590 */
2591 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002592compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002593{
2594 type_T *want_type = NULL;
2595
2596 // Recognize <type>
2597 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2598 {
2599 ++*arg;
2600 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2601 if (want_type == NULL)
2602 return FAIL;
2603
2604 if (**arg != '>')
2605 {
2606 if (*skipwhite(*arg) == '>')
2607 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2608 else
2609 emsg(_(e_missing_gt));
2610 return FAIL;
2611 }
2612 ++*arg;
2613 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2614 return FAIL;
2615 }
2616
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002617 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002618 return FAIL;
2619
2620 if (want_type != NULL)
2621 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002622 type_T *actual;
2623 where_T where = WHERE_INIT;
2624
2625 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002626 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002627 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002628 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002629 if (need_type(actual, want_type, FALSE,
2630 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002631 return FAIL;
2632 }
2633 }
2634
2635 return OK;
2636}
2637
2638/*
2639 * * number multiplication
2640 * / number division
2641 * % number modulo
2642 */
2643 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002644compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002645{
2646 char_u *op;
2647 char_u *next;
2648 int ppconst_used = ppconst->pp_used;
2649
2650 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002651 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002652 return FAIL;
2653
2654 /*
2655 * Repeat computing, until no "*", "/" or "%" is following.
2656 */
2657 for (;;)
2658 {
2659 op = may_peek_next_line(cctx, *arg, &next);
2660 if (*op != '*' && *op != '/' && *op != '%')
2661 break;
2662 if (next != NULL)
2663 {
2664 *arg = next_line_from_context(cctx, TRUE);
2665 op = skipwhite(*arg);
2666 }
2667
2668 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2669 {
2670 error_white_both(op, 1);
2671 return FAIL;
2672 }
2673 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2674 return FAIL;
2675
2676 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002677 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002678 return FAIL;
2679
2680 if (ppconst->pp_used == ppconst_used + 2
2681 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2682 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2683 {
2684 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2685 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2686 varnumber_T res = 0;
2687 int failed = FALSE;
2688
2689 // both are numbers: compute the result
2690 switch (*op)
2691 {
2692 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2693 break;
2694 case '/': res = num_divide(tv1->vval.v_number,
2695 tv2->vval.v_number, &failed);
2696 break;
2697 case '%': res = num_modulus(tv1->vval.v_number,
2698 tv2->vval.v_number, &failed);
2699 break;
2700 }
2701 if (failed)
2702 return FAIL;
2703 tv1->vval.v_number = res;
2704 --ppconst->pp_used;
2705 }
2706 else
2707 {
2708 generate_ppconst(cctx, ppconst);
2709 generate_two_op(cctx, op);
2710 }
2711 }
2712
2713 return OK;
2714}
2715
2716/*
2717 * + number addition or list/blobl concatenation
2718 * - number subtraction
2719 * .. string concatenation
2720 */
2721 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002722compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002723{
2724 char_u *op;
2725 char_u *next;
2726 int oplen;
2727 int ppconst_used = ppconst->pp_used;
2728
2729 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002730 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002731 return FAIL;
2732
2733 /*
2734 * Repeat computing, until no "+", "-" or ".." is following.
2735 */
2736 for (;;)
2737 {
2738 op = may_peek_next_line(cctx, *arg, &next);
2739 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2740 break;
2741 if (op[0] == op[1] && *op != '.' && next)
2742 // Finding "++" or "--" on the next line is a separate command.
2743 // But ".." is concatenation.
2744 break;
2745 oplen = (*op == '.' ? 2 : 1);
2746 if (next != NULL)
2747 {
2748 *arg = next_line_from_context(cctx, TRUE);
2749 op = skipwhite(*arg);
2750 }
2751
2752 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2753 {
2754 error_white_both(op, oplen);
2755 return FAIL;
2756 }
2757
2758 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2759 return FAIL;
2760
2761 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002762 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002763 return FAIL;
2764
2765 if (ppconst->pp_used == ppconst_used + 2
2766 && (*op == '.'
2767 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2768 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2769 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2770 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2771 {
2772 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2773 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2774
2775 // concat/subtract/add constant numbers
2776 if (*op == '+')
2777 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2778 else if (*op == '-')
2779 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2780 else
2781 {
2782 // concatenate constant strings
2783 char_u *s1 = tv1->vval.v_string;
2784 char_u *s2 = tv2->vval.v_string;
2785 size_t len1 = STRLEN(s1);
2786
2787 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2788 if (tv1->vval.v_string == NULL)
2789 {
2790 clear_ppconst(ppconst);
2791 return FAIL;
2792 }
2793 mch_memmove(tv1->vval.v_string, s1, len1);
2794 STRCPY(tv1->vval.v_string + len1, s2);
2795 vim_free(s1);
2796 vim_free(s2);
2797 }
2798 --ppconst->pp_used;
2799 }
2800 else
2801 {
2802 generate_ppconst(cctx, ppconst);
2803 ppconst->pp_is_const = FALSE;
2804 if (*op == '.')
2805 {
2806 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2807 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2808 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002809 if (generate_CONCAT(cctx, 2) == FAIL)
2810 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002811 }
2812 else
2813 generate_two_op(cctx, op);
2814 }
2815 }
2816
2817 return OK;
2818}
2819
2820/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002821 * expr6a >> expr6b
2822 * expr6a << expr6b
2823 *
2824 * Produces instructions:
2825 * OPNR bitwise left or right shift
2826 */
2827 static int
2828compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2829{
2830 exprtype_T type = EXPR_UNKNOWN;
2831 char_u *p;
2832 char_u *next;
2833 int len = 2;
2834 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002835 isn_T *isn;
2836
2837 // get the first variable
2838 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2839 return FAIL;
2840
2841 /*
2842 * Repeat computing, until no "+", "-" or ".." is following.
2843 */
2844 for (;;)
2845 {
2846 type = EXPR_UNKNOWN;
2847
2848 p = may_peek_next_line(cctx, *arg, &next);
2849 if (p[0] == '<' && p[1] == '<')
2850 type = EXPR_LSHIFT;
2851 else if (p[0] == '>' && p[1] == '>')
2852 type = EXPR_RSHIFT;
2853
2854 if (type == EXPR_UNKNOWN)
2855 return OK;
2856
2857 // Handle a bitwise left or right shift operator
2858 if (ppconst->pp_used == ppconst_used + 1)
2859 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002860 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002861 {
2862 // left operand should be a number
2863 emsg(_(e_bitshift_ops_must_be_number));
2864 return FAIL;
2865 }
2866 }
2867 else
2868 {
2869 type_T *t = get_type_on_stack(cctx, 0);
2870
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002871 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002872 {
2873 emsg(_(e_bitshift_ops_must_be_number));
2874 return FAIL;
2875 }
2876 }
2877
2878 if (next != NULL)
2879 {
2880 *arg = next_line_from_context(cctx, TRUE);
2881 p = skipwhite(*arg);
2882 }
2883
2884 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2885 {
2886 error_white_both(p, len);
2887 return FAIL;
2888 }
2889
2890 // get the second variable
2891 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2892 return FAIL;
2893
2894 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2895 return FAIL;
2896
2897 if (ppconst->pp_used == ppconst_used + 2)
2898 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002899 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2900 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2901
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002902 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002903 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2904 {
2905 // right operand should be a positive number
2906 if (tv2->v_type != VAR_NUMBER)
2907 emsg(_(e_bitshift_ops_must_be_number));
2908 else
dundargocc57b5bc2022-11-02 13:30:51 +00002909 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002910 return FAIL;
2911 }
2912
2913 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2914 tv1->vval.v_number = 0;
2915 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002916 tv1->vval.v_number =
2917 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002918 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002919 tv1->vval.v_number =
2920 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002921 clear_tv(tv2);
2922 --ppconst->pp_used;
2923 }
2924 else
2925 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002926 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2927 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002928 {
2929 emsg(_(e_bitshift_ops_must_be_number));
2930 return FAIL;
2931 }
2932
2933 generate_ppconst(cctx, ppconst);
2934
2935 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2936 if (isn == NULL)
2937 return FAIL;
2938
2939 if (isn != NULL)
2940 isn->isn_arg.op.op_type = type;
2941 }
2942 }
2943
2944 return OK;
2945}
2946
2947/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002948 * expr5a == expr5b
2949 * expr5a =~ expr5b
2950 * expr5a != expr5b
2951 * expr5a !~ expr5b
2952 * expr5a > expr5b
2953 * expr5a >= expr5b
2954 * expr5a < expr5b
2955 * expr5a <= expr5b
2956 * expr5a is expr5b
2957 * expr5a isnot expr5b
2958 *
2959 * Produces instructions:
2960 * EVAL expr5a Push result of "expr5a"
2961 * EVAL expr5b Push result of "expr5b"
2962 * COMPARE one of the compare instructions
2963 */
2964 static int
2965compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2966{
2967 exprtype_T type = EXPR_UNKNOWN;
2968 char_u *p;
2969 char_u *next;
2970 int len = 2;
2971 int type_is = FALSE;
2972 int ppconst_used = ppconst->pp_used;
2973
2974 // get the first variable
2975 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2976 return FAIL;
2977
2978 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002979
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002980 type = get_compare_type(p, &len, &type_is);
2981
2982 /*
2983 * If there is a comparative operator, use it.
2984 */
2985 if (type != EXPR_UNKNOWN)
2986 {
2987 int ic = FALSE; // Default: do not ignore case
2988
2989 if (next != NULL)
2990 {
2991 *arg = next_line_from_context(cctx, TRUE);
2992 p = skipwhite(*arg);
2993 }
2994 if (type_is && (p[len] == '?' || p[len] == '#'))
2995 {
2996 semsg(_(e_invalid_expression_str), *arg);
2997 return FAIL;
2998 }
2999 // extra question mark appended: ignore case
3000 if (p[len] == '?')
3001 {
3002 ic = TRUE;
3003 ++len;
3004 }
3005 // extra '#' appended: match case (ignored)
3006 else if (p[len] == '#')
3007 ++len;
3008 // nothing appended: match case
3009
3010 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
3011 {
3012 error_white_both(p, len);
3013 return FAIL;
3014 }
3015
3016 // get the second variable
3017 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
3018 return FAIL;
3019
3020 if (compile_expr5(arg, cctx, ppconst) == FAIL)
3021 return FAIL;
3022
3023 if (ppconst->pp_used == ppconst_used + 2)
3024 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01003025 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003026 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
3027 int ret;
3028
3029 // Both sides are a constant, compute the result now.
3030 // First check for a valid combination of types, this is more
3031 // strict than typval_compare().
3032 if (check_compare_types(type, tv1, tv2) == FAIL)
3033 ret = FAIL;
3034 else
3035 {
3036 ret = typval_compare(tv1, tv2, type, ic);
3037 tv1->v_type = VAR_BOOL;
3038 tv1->vval.v_number = tv1->vval.v_number
3039 ? VVAL_TRUE : VVAL_FALSE;
3040 clear_tv(tv2);
3041 --ppconst->pp_used;
3042 }
3043 return ret;
3044 }
3045
3046 generate_ppconst(cctx, ppconst);
3047 return generate_COMPARE(cctx, type, ic);
3048 }
3049
3050 return OK;
3051}
3052
3053static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
3054
3055/*
3056 * Compile || or &&.
3057 */
3058 static int
3059compile_and_or(
3060 char_u **arg,
3061 cctx_T *cctx,
3062 char *op,
3063 ppconst_T *ppconst,
3064 int ppconst_used UNUSED)
3065{
3066 char_u *next;
3067 char_u *p = may_peek_next_line(cctx, *arg, &next);
3068 int opchar = *op;
3069
3070 if (p[0] == opchar && p[1] == opchar)
3071 {
3072 garray_T *instr = &cctx->ctx_instr;
3073 garray_T end_ga;
3074 int save_skip = cctx->ctx_skip;
3075
3076 /*
3077 * Repeat until there is no following "||" or "&&"
3078 */
3079 ga_init2(&end_ga, sizeof(int), 10);
3080 while (p[0] == opchar && p[1] == opchar)
3081 {
3082 long start_lnum = SOURCING_LNUM;
3083 long save_sourcing_lnum;
3084 int start_ctx_lnum = cctx->ctx_lnum;
3085 int save_lnum;
3086 int const_used;
3087 int status;
3088 jumpwhen_T jump_when = opchar == '|'
3089 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
3090
3091 if (next != NULL)
3092 {
3093 *arg = next_line_from_context(cctx, TRUE);
3094 p = skipwhite(*arg);
3095 }
3096
3097 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
3098 {
3099 semsg(_(e_white_space_required_before_and_after_str_at_str),
3100 op, p);
3101 ga_clear(&end_ga);
3102 return FAIL;
3103 }
3104
3105 save_sourcing_lnum = SOURCING_LNUM;
3106 SOURCING_LNUM = start_lnum;
3107 save_lnum = cctx->ctx_lnum;
3108 cctx->ctx_lnum = start_ctx_lnum;
3109
3110 status = check_ppconst_bool(ppconst);
3111 if (status != FAIL)
3112 {
3113 // Use the last ppconst if possible.
3114 if (ppconst->pp_used > 0)
3115 {
3116 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3117 int is_true = tv2bool(tv);
3118
3119 if ((is_true && opchar == '|')
3120 || (!is_true && opchar == '&'))
3121 {
3122 // For "false && expr" and "true || expr" the "expr"
3123 // does not need to be evaluated.
3124 cctx->ctx_skip = SKIP_YES;
3125 clear_tv(tv);
3126 tv->v_type = VAR_BOOL;
3127 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3128 }
3129 else
3130 {
3131 // For "true && expr" and "false || expr" only "expr"
3132 // needs to be evaluated.
3133 --ppconst->pp_used;
3134 jump_when = JUMP_NEVER;
3135 }
3136 }
3137 else
3138 {
3139 // Every part must evaluate to a bool.
3140 status = bool_on_stack(cctx);
3141 }
3142 }
3143 if (status != FAIL)
3144 status = ga_grow(&end_ga, 1);
3145 cctx->ctx_lnum = save_lnum;
3146 if (status == FAIL)
3147 {
3148 ga_clear(&end_ga);
3149 return FAIL;
3150 }
3151
3152 if (jump_when != JUMP_NEVER)
3153 {
3154 if (cctx->ctx_skip != SKIP_YES)
3155 {
3156 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3157 ++end_ga.ga_len;
3158 }
3159 generate_JUMP(cctx, jump_when, 0);
3160 }
3161
3162 // eval the next expression
3163 SOURCING_LNUM = save_sourcing_lnum;
3164 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3165 {
3166 ga_clear(&end_ga);
3167 return FAIL;
3168 }
3169
3170 const_used = ppconst->pp_used;
3171 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3172 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3173 {
3174 ga_clear(&end_ga);
3175 return FAIL;
3176 }
3177
3178 // "0 || 1" results in true, "1 && 0" results in false.
3179 if (ppconst->pp_used == const_used + 1)
3180 {
3181 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3182
3183 if (tv->v_type == VAR_NUMBER
3184 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3185 {
3186 tv->vval.v_number = tv->vval.v_number == 1
3187 ? VVAL_TRUE : VVAL_FALSE;
3188 tv->v_type = VAR_BOOL;
3189 }
3190 }
3191
3192 p = may_peek_next_line(cctx, *arg, &next);
3193 }
3194
3195 if (check_ppconst_bool(ppconst) == FAIL)
3196 {
3197 ga_clear(&end_ga);
3198 return FAIL;
3199 }
3200
3201 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3202 // Every part must evaluate to a bool.
3203 if (bool_on_stack(cctx) == FAIL)
3204 {
3205 ga_clear(&end_ga);
3206 return FAIL;
3207 }
3208
3209 if (end_ga.ga_len > 0)
3210 {
3211 // Fill in the end label in all jumps.
3212 generate_ppconst(cctx, ppconst);
3213 while (end_ga.ga_len > 0)
3214 {
3215 isn_T *isn;
3216
3217 --end_ga.ga_len;
3218 isn = ((isn_T *)instr->ga_data)
3219 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3220 isn->isn_arg.jump.jump_where = instr->ga_len;
3221 }
3222 }
3223 ga_clear(&end_ga);
3224
3225 cctx->ctx_skip = save_skip;
3226 }
3227
3228 return OK;
3229}
3230
3231/*
3232 * expr4a && expr4a && expr4a logical AND
3233 *
3234 * Produces instructions:
3235 * EVAL expr4a Push result of "expr4a"
3236 * COND2BOOL convert to bool if needed
3237 * JUMP_IF_COND_FALSE end
3238 * EVAL expr4b Push result of "expr4b"
3239 * JUMP_IF_COND_FALSE end
3240 * EVAL expr4c Push result of "expr4c"
3241 * end:
3242 */
3243 static int
3244compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3245{
3246 int ppconst_used = ppconst->pp_used;
3247
3248 // get the first variable
3249 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3250 return FAIL;
3251
3252 // || and && work almost the same
3253 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3254}
3255
3256/*
3257 * expr3a || expr3b || expr3c logical OR
3258 *
3259 * Produces instructions:
3260 * EVAL expr3a Push result of "expr3a"
3261 * COND2BOOL convert to bool if needed
3262 * JUMP_IF_COND_TRUE end
3263 * EVAL expr3b Push result of "expr3b"
3264 * JUMP_IF_COND_TRUE end
3265 * EVAL expr3c Push result of "expr3c"
3266 * end:
3267 */
3268 static int
3269compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3270{
3271 int ppconst_used = ppconst->pp_used;
3272
3273 // eval the first expression
3274 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3275 return FAIL;
3276
3277 // || and && work almost the same
3278 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3279}
3280
3281/*
3282 * Toplevel expression: expr2 ? expr1a : expr1b
3283 * Produces instructions:
3284 * EVAL expr2 Push result of "expr2"
3285 * JUMP_IF_FALSE alt jump if false
3286 * EVAL expr1a
3287 * JUMP_ALWAYS end
3288 * alt: EVAL expr1b
3289 * end:
3290 *
3291 * Toplevel expression: expr2 ?? expr1
3292 * Produces instructions:
3293 * EVAL expr2 Push result of "expr2"
3294 * JUMP_AND_KEEP_IF_TRUE end jump if true
3295 * EVAL expr1
3296 * end:
3297 */
3298 int
3299compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3300{
3301 char_u *p;
3302 int ppconst_used = ppconst->pp_used;
3303 char_u *next;
3304
3305 // Ignore all kinds of errors when not producing code.
3306 if (cctx->ctx_skip == SKIP_YES)
3307 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003308 int prev_did_emsg = did_emsg;
3309
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003310 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003311 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003312 }
3313
3314 // Evaluate the first expression.
3315 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3316 return FAIL;
3317
3318 p = may_peek_next_line(cctx, *arg, &next);
3319 if (*p == '?')
3320 {
3321 int op_falsy = p[1] == '?';
3322 garray_T *instr = &cctx->ctx_instr;
3323 garray_T *stack = &cctx->ctx_type_stack;
3324 int alt_idx = instr->ga_len;
3325 int end_idx = 0;
3326 isn_T *isn;
3327 type_T *type1 = NULL;
3328 int has_const_expr = FALSE;
3329 int const_value = FALSE;
3330 int save_skip = cctx->ctx_skip;
3331
3332 if (next != NULL)
3333 {
3334 *arg = next_line_from_context(cctx, TRUE);
3335 p = skipwhite(*arg);
3336 }
3337
3338 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3339 {
3340 semsg(_(e_white_space_required_before_and_after_str_at_str),
3341 op_falsy ? "??" : "?", p);
3342 return FAIL;
3343 }
3344
3345 if (ppconst->pp_used == ppconst_used + 1)
3346 {
3347 // the condition is a constant, we know whether the ? or the :
3348 // expression is to be evaluated.
3349 has_const_expr = TRUE;
3350 if (op_falsy)
3351 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3352 else
3353 {
3354 int error = FALSE;
3355
3356 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3357 &error);
3358 if (error)
3359 return FAIL;
3360 }
3361 cctx->ctx_skip = save_skip == SKIP_YES ||
3362 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3363
3364 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3365 // "left ?? right" and "left" is truthy: produce "left"
3366 generate_ppconst(cctx, ppconst);
3367 else
3368 {
3369 clear_tv(&ppconst->pp_tv[ppconst_used]);
3370 --ppconst->pp_used;
3371 }
3372 }
3373 else
3374 {
3375 generate_ppconst(cctx, ppconst);
3376 if (op_falsy)
3377 end_idx = instr->ga_len;
3378 generate_JUMP(cctx, op_falsy
3379 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3380 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003381 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003382 }
3383
3384 // evaluate the second expression; any type is accepted
3385 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3386 return FAIL;
3387 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3388 return FAIL;
3389
3390 if (!has_const_expr)
3391 {
3392 generate_ppconst(cctx, ppconst);
3393
3394 if (!op_falsy)
3395 {
3396 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003397 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003398 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003399
3400 end_idx = instr->ga_len;
3401 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3402
3403 // jump here from JUMP_IF_FALSE
3404 isn = ((isn_T *)instr->ga_data) + alt_idx;
3405 isn->isn_arg.jump.jump_where = instr->ga_len;
3406 }
3407 }
3408
3409 if (!op_falsy)
3410 {
3411 // Check for the ":".
3412 p = may_peek_next_line(cctx, *arg, &next);
3413 if (*p != ':')
3414 {
3415 emsg(_(e_missing_colon_after_questionmark));
3416 return FAIL;
3417 }
3418 if (next != NULL)
3419 {
3420 *arg = next_line_from_context(cctx, TRUE);
3421 p = skipwhite(*arg);
3422 }
3423
3424 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3425 {
3426 semsg(_(e_white_space_required_before_and_after_str_at_str),
3427 ":", p);
3428 return FAIL;
3429 }
3430
3431 // evaluate the third expression
3432 if (has_const_expr)
3433 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3434 ? SKIP_YES : SKIP_NOT;
3435 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3436 return FAIL;
3437 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3438 return FAIL;
3439 }
3440
3441 if (!has_const_expr)
3442 {
3443 type_T **typep;
3444
3445 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003446 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003447
3448 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003449 typep = &((((type2_T *)stack->ga_data)
3450 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003451 common_type(type1, *typep, typep, cctx->ctx_type_list);
3452
3453 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3454 isn = ((isn_T *)instr->ga_data) + end_idx;
3455 isn->isn_arg.jump.jump_where = instr->ga_len;
3456 }
3457
3458 cctx->ctx_skip = save_skip;
3459 }
3460 return OK;
3461}
3462
3463/*
3464 * Toplevel expression.
3465 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3466 * Returns OK or FAIL.
3467 */
3468 int
3469compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3470{
3471 ppconst_T ppconst;
3472
3473 CLEAR_FIELD(ppconst);
3474 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3475 {
3476 clear_ppconst(&ppconst);
3477 return FAIL;
3478 }
3479 if (is_const != NULL)
3480 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3481 if (generate_ppconst(cctx, &ppconst) == FAIL)
3482 return FAIL;
3483 return OK;
3484}
3485
3486/*
3487 * Toplevel expression.
3488 */
3489 int
3490compile_expr0(char_u **arg, cctx_T *cctx)
3491{
3492 return compile_expr0_ext(arg, cctx, NULL);
3493}
3494
3495
3496#endif // defined(FEAT_EVAL)