blob: c5897faf8bedfc7fa9d352e110f53e3d708c9424 [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
266 ++*arg;
267 char_u *name = *arg;
268 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
269 if (name_end == name)
270 return FAIL;
271 size_t len = name_end - name;
272
273 class_T *cl = (class_T *)type->tt_member;
274 if (*name_end == '(')
275 {
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000276 // TODO: method or function call
277 emsg("compile_class_object_index(): object/class call not handled yet");
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000278 }
279 else if (type->tt_type == VAR_OBJECT)
280 {
281 for (int i = 0; i < cl->class_obj_member_count; ++i)
282 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000283 ocmember_T *m = &cl->class_obj_members[i];
284 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000285 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000286 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
287 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000288 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000289 return FAIL;
290 }
291
Bram Moolenaard505d172022-12-18 21:42:55 +0000292 generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000293
294 *arg = name_end;
295 return OK;
296 }
297 }
298
299 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
300 }
301 else
302 {
303 // TODO: class member
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000304 emsg("compile_class_object_index(): class member not handled yet");
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000305 }
306
307 return FAIL;
308}
309
310/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000311 * Generate an instruction to load script-local variable "name", without the
312 * leading "s:".
313 * Also finds imported variables.
314 */
315 int
316compile_load_scriptvar(
317 cctx_T *cctx,
318 char_u *name, // variable NUL terminated
319 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100320 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000321{
322 scriptitem_T *si;
323 int idx;
324 imported_T *import;
325
326 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
327 return FAIL;
328 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000329 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000330 if (idx >= 0)
331 {
332 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
333
334 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
335 current_sctx.sc_sid, idx, sv->sv_type);
336 return OK;
337 }
338
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000339 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000340 if (import != NULL)
341 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000342 char_u *p = skipwhite(*end);
343 char_u *exp_name;
344 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100345 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000346 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000347 int done = FALSE;
348 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000349
350 // Need to lookup the member.
351 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000352 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000353 semsg(_(e_expected_dot_after_name_str), start);
354 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000355 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000356 ++p;
357 if (VIM_ISWHITE(*p))
358 {
359 emsg(_(e_no_white_space_allowed_after_dot));
360 return FAIL;
361 }
362
363 // isolate one name
364 exp_name = p;
365 while (eval_isnamec(*p))
366 ++p;
367 cc = *p;
368 *p = NUL;
369
Bram Moolenaard041f422022-01-12 19:54:00 +0000370 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100371 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
372 // "import autoload './dir/script.vim'" or
373 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100374 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100375
376 if (res == OK)
377 {
378 if (si->sn_autoload_prefix != NULL
379 && si->sn_state == SN_STATE_NOT_LOADED)
380 {
381 char_u *auto_name =
382 concat_str(si->sn_autoload_prefix, exp_name);
383
384 // autoload script must be loaded later, access by the autoload
385 // name. If a '(' follows it must be a function. Otherwise we
386 // don't know, it can be "script.Func".
387 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100388 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100389 else
390 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
391 vim_free(auto_name);
392 done = TRUE;
393 }
394 else if (si->sn_import_autoload
395 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100396 {
397 // If a '(' follows it must be a function. Otherwise we don't
398 // know, it can be "script.Func".
399 if (cc == '(' || paren_follows_after_expr)
400 {
401 char_u sid_name[MAX_FUNC_NAME_LEN];
402
403 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100404 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100405 }
406 else
407 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
408 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100409 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100410 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100411 else
412 {
413 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
414 cctx, NULL, TRUE);
415 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100416 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100417
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000418 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000419 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000420 if (done)
421 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000422
423 if (idx < 0)
424 {
425 if (ufunc != NULL)
426 {
427 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100428 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000429 return OK;
430 }
431 return FAIL;
432 }
433
434 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
435 import->imp_sid,
436 idx,
437 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000438 return OK;
439 }
440
Bram Moolenaard0132f42022-05-12 11:05:40 +0100441 // Can only get here if we know "name" is a script variable and not in a
442 // Vim9 script (variable is not in sn_var_vals): old style script.
443 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000444 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000445}
446
447 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000448generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000449{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000450 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000451 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000452
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000453 // Reject a global non-autoload function found without the "g:" prefix.
454 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000455 return FAIL;
456
457 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000458 compile_type = get_compile_type(ufunc);
459 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000460 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000461 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100462 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000463}
464
465/*
466 * Compile a variable name into a load instruction.
467 * "end" points to just after the name.
468 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
469 * When "error" is FALSE do not give an error when not found.
470 */
471 int
472compile_load(
473 char_u **arg,
474 char_u *end_arg,
475 cctx_T *cctx,
476 int is_expr,
477 int error)
478{
479 type_T *type;
480 char_u *name = NULL;
481 char_u *end = end_arg;
482 int res = FAIL;
483 int prev_called_emsg = called_emsg;
484
485 if (*(*arg + 1) == ':')
486 {
487 if (end <= *arg + 2)
488 {
489 isntype_T isn_type;
490
491 // load dictionary of namespace
492 switch (**arg)
493 {
494 case 'g': isn_type = ISN_LOADGDICT; break;
495 case 'w': isn_type = ISN_LOADWDICT; break;
496 case 't': isn_type = ISN_LOADTDICT; break;
497 case 'b': isn_type = ISN_LOADBDICT; break;
498 default:
499 semsg(_(e_namespace_not_supported_str), *arg);
500 goto theend;
501 }
502 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
503 goto theend;
504 res = OK;
505 }
506 else
507 {
508 isntype_T isn_type = ISN_DROP;
509
510 // load namespaced variable
511 name = vim_strnsave(*arg + 2, end - (*arg + 2));
512 if (name == NULL)
513 return FAIL;
514
515 switch (**arg)
516 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100517 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000518 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000519 case 's': if (current_script_is_vim9())
520 {
521 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
522 *arg);
523 vim_free(name);
524 return FAIL;
525 }
Kota Kato948a3892022-08-16 16:09:59 +0100526 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000527 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000528 else
529 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100530 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000531 break;
532 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
533 {
534 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000535 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000536 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000537 else
538 isn_type = ISN_LOADG;
539 }
540 else
541 {
542 isn_type = ISN_LOADAUTO;
543 vim_free(name);
544 name = vim_strnsave(*arg, end - *arg);
545 if (name == NULL)
546 return FAIL;
547 }
548 break;
549 case 'w': isn_type = ISN_LOADW; break;
550 case 't': isn_type = ISN_LOADT; break;
551 case 'b': isn_type = ISN_LOADB; break;
552 default: // cannot happen, just in case
553 semsg(_(e_namespace_not_supported_str), *arg);
554 goto theend;
555 }
556 if (isn_type != ISN_DROP)
557 {
558 // Global, Buffer-local, Window-local and Tabpage-local
559 // variables can be defined later, thus we don't check if it
560 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000561 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000562 }
563 }
564 }
565 else
566 {
567 size_t len = end - *arg;
568 int idx;
569 int gen_load = FALSE;
570 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100571 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100572 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000573
574 name = vim_strnsave(*arg, end - *arg);
575 if (name == NULL)
576 return FAIL;
577
578 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
579 {
580 script_autoload(name, FALSE);
581 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
582 }
583 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
584 == OK)
585 {
586 if (gen_load_outer == 0)
587 gen_load = TRUE;
588 }
589 else
590 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000591 lvar_T lvar;
592 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000593
594 if (lookup_local(*arg, len, &lvar, cctx) == OK)
595 {
596 type = lvar.lv_type;
597 idx = lvar.lv_idx;
598 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100599 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000600 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100601 outer_loop_depth = lvar.lv_loop_depth;
602 outer_loop_idx = lvar.lv_loop_idx;
603 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000604 else
605 gen_load = TRUE;
606 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000607 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000608 {
609 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
610 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000611 else
612 {
613 // "var" can be script-local even without using "s:" if it
614 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000615 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000616 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100617 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000618
619 // When evaluating an expression and the name starts with an
620 // uppercase letter it can be a user defined function.
621 // generate_funcref() will fail if the function can't be found.
622 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000623 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000624 }
625 }
626 if (gen_load)
627 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
628 if (gen_load_outer > 0)
629 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100630 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
631 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000632 cctx->ctx_outer_used = TRUE;
633 }
634 }
635
636 *arg = end;
637
638theend:
639 if (res == FAIL && error && called_emsg == prev_called_emsg)
640 semsg(_(e_variable_not_found_str), name);
641 vim_free(name);
642 return res;
643}
644
645/*
646 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100647 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000648 * Returns FAIL if compilation fails.
649 */
650 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100651compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000652{
LemonBoyf3b48952022-05-05 13:53:03 +0100653 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000654 garray_T save_ga = cctx->ctx_instr;
655 int expr_res;
656 int trailing_error;
657 int instr_count;
658 isn_T *instr = NULL;
659
660 // Remove the string type from the stack.
661 --cctx->ctx_type_stack.ga_len;
662
663 // Temporarily reset the list of instructions so that the jump labels are
664 // correct.
665 cctx->ctx_instr.ga_len = 0;
666 cctx->ctx_instr.ga_maxlen = 0;
667 cctx->ctx_instr.ga_data = NULL;
668 expr_res = compile_expr0(&s, cctx);
669 s = skipwhite(s);
670 trailing_error = *s != NUL;
671
672 if (expr_res == FAIL || trailing_error
673 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
674 {
675 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000676 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000677 clear_instr_ga(&cctx->ctx_instr);
678 cctx->ctx_instr = save_ga;
679 ++cctx->ctx_type_stack.ga_len;
680 return FAIL;
681 }
682
683 // Move the generated instructions into the ISN_INSTR instruction, then
684 // restore the list of instructions.
685 instr_count = cctx->ctx_instr.ga_len;
686 instr = cctx->ctx_instr.ga_data;
687 instr[instr_count].isn_type = ISN_FINISH;
688
689 cctx->ctx_instr = save_ga;
690 vim_free(isn->isn_arg.string);
691 isn->isn_type = ISN_INSTR;
692 isn->isn_arg.instr = instr;
693 return OK;
694}
695
696/*
697 * Compile the argument expressions.
698 * "arg" points to just after the "(" and is advanced to after the ")"
699 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100700 int
LemonBoyf3b48952022-05-05 13:53:03 +0100701compile_arguments(
702 char_u **arg,
703 cctx_T *cctx,
704 int *argcount,
705 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000706{
707 char_u *p = *arg;
708 char_u *whitep = *arg;
709 int must_end = FALSE;
710 int instr_count;
711
712 for (;;)
713 {
714 if (may_get_next_line(whitep, &p, cctx) == FAIL)
715 goto failret;
716 if (*p == ')')
717 {
718 *arg = p + 1;
719 return OK;
720 }
721 if (must_end)
722 {
723 semsg(_(e_missing_comma_before_argument_str), p);
724 return FAIL;
725 }
726
727 instr_count = cctx->ctx_instr.ga_len;
728 if (compile_expr0(&p, cctx) == FAIL)
729 return FAIL;
730 ++*argcount;
731
LemonBoyf3b48952022-05-05 13:53:03 +0100732 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000733 && cctx->ctx_instr.ga_len == instr_count + 1)
734 {
735 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
736
737 // {skip} argument of searchpair() can be compiled if not empty
738 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100739 compile_string(isn, cctx, 0);
740 }
741 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
742 && cctx->ctx_instr.ga_len == instr_count + 1)
743 {
744 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
745
746 // {sub} argument of substitute() can be compiled if it starts
747 // with \=
748 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100749 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100750 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000751 }
752
753 if (*p != ',' && *skipwhite(p) == ',')
754 {
755 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
756 p = skipwhite(p);
757 }
758 if (*p == ',')
759 {
760 ++p;
761 if (*p != NUL && !VIM_ISWHITE(*p))
762 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
763 }
764 else
765 must_end = TRUE;
766 whitep = p;
767 p = skipwhite(p);
768 }
769failret:
770 emsg(_(e_missing_closing_paren));
771 return FAIL;
772}
773
774/*
775 * Compile a function call: name(arg1, arg2)
776 * "arg" points to "name", "arg + varlen" to the "(".
777 * "argcount_init" is 1 for "value->method()"
778 * Instructions:
779 * EVAL arg1
780 * EVAL arg2
781 * BCALL / DCALL / UCALL
782 */
783 static int
784compile_call(
785 char_u **arg,
786 size_t varlen,
787 cctx_T *cctx,
788 ppconst_T *ppconst,
789 int argcount_init)
790{
791 char_u *name = *arg;
792 char_u *p;
793 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100794 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000795 char_u fname_buf[FLEN_FIXED + 1];
796 char_u *tofree = NULL;
797 int error = FCERR_NONE;
798 ufunc_T *ufunc = NULL;
799 int res = FAIL;
800 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000801 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100802 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000803 imported_T *import;
804
805 if (varlen >= sizeof(namebuf))
806 {
807 semsg(_(e_name_too_long_str), name);
808 return FAIL;
809 }
810 vim_strncpy(namebuf, *arg, varlen);
811
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000812 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000813 if (import != NULL)
814 {
815 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
816 return FAIL;
817 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000818
819 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100820 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000821 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100822 if ((varlen == 3
823 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000824 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
825 {
826 char_u *s = skipwhite(*arg + varlen + 1);
827 typval_T argvars[2];
828 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100829 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000830
831 argvars[0].v_type = VAR_UNKNOWN;
832 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100833 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000834 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100835 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000836 s = skipwhite(s);
837 if (*s == ')' && argvars[0].v_type == VAR_STRING
838 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
839 || !is_has))
840 {
841 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
842
843 *arg = s + 1;
844 argvars[1].v_type = VAR_UNKNOWN;
845 tv->v_type = VAR_NUMBER;
846 tv->vval.v_number = 0;
847 if (is_has)
848 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100849 else if (is_len)
850 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000851 else
852 f_exists(argvars, tv);
853 clear_tv(&argvars[0]);
854 ++ppconst->pp_used;
855 return OK;
856 }
857 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100858 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000859 {
860 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
861 return FAIL;
862 }
863 }
864
865 if (generate_ppconst(cctx, ppconst) == FAIL)
866 return FAIL;
867
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000868 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
869
870 // We handle the "skip" argument of searchpair() and searchpairpos()
871 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100872 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
873 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
874 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
875 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
876 special_fn = CA_SEARCHPAIR;
877 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
878 special_fn = CA_SUBSTITUTE;
879 else
880 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000881
882 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100883 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000884 goto theend;
885
886 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
887 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
888 {
889 int idx;
890
891 // builtin function
892 idx = find_internal_func(name);
893 if (idx >= 0)
894 {
895 if (STRCMP(name, "flatten") == 0)
896 {
897 emsg(_(e_cannot_use_flatten_in_vim9_script));
898 goto theend;
899 }
900
901 if (STRCMP(name, "add") == 0 && argcount == 2)
902 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000903 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000904
905 // add() can be compiled to instructions if we know the type
906 if (type->tt_type == VAR_LIST)
907 {
908 // inline "add(list, item)" so that the type can be checked
909 res = generate_LISTAPPEND(cctx);
910 idx = -1;
911 }
912 else if (type->tt_type == VAR_BLOB)
913 {
914 // inline "add(blob, nr)" so that the type can be checked
915 res = generate_BLOBAPPEND(cctx);
916 idx = -1;
917 }
918 }
919
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100920 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
921 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100922 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100923 // May have the "D" or "R" flag, reserve a variable for a
924 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100925 if (get_defer_var_idx(cctx) == 0)
926 idx = -1;
927 }
928
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000929 if (idx >= 0)
930 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
931 }
932 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100933 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000934 goto theend;
935 }
936
Bram Moolenaar62aec932022-01-29 21:45:34 +0000937 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
938
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000939 // An argument or local variable can be a function reference, this
940 // overrules a function name.
941 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
942 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
943 {
944 // If we can find the function by name generate the right call.
945 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000946 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000947 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000948 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000949 if (!func_is_global(ufunc))
950 {
951 res = generate_CALL(cctx, ufunc, argcount);
952 goto theend;
953 }
954 if (!has_g_namespace
955 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
956 {
957 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100958 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000959 goto theend;
960 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000961 }
962 }
963
964 // If the name is a variable, load it and use PCALL.
965 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000966 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000967 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000968 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000969 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
970 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000971 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000972
973 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
974 goto theend;
975 }
976
977 // If we can find a global function by name generate the right call.
978 if (ufunc != NULL)
979 {
980 res = generate_CALL(cctx, ufunc, argcount);
981 goto theend;
982 }
983
984 // A global function may be defined only later. Need to figure out at
985 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000986 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000987 res = generate_UCALL(cctx, name, argcount);
988 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100989 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000990
991theend:
992 vim_free(tofree);
993 return res;
994}
995
996// like NAMESPACE_CHAR but with 'a' and 'l'.
997#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
998
999/*
1000 * Find the end of a variable or function name. Unlike find_name_end() this
1001 * does not recognize magic braces.
1002 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1003 * Return a pointer to just after the name. Equal to "arg" if there is no
1004 * valid name.
1005 */
1006 char_u *
1007to_name_end(char_u *arg, int use_namespace)
1008{
1009 char_u *p;
1010
1011 // Quick check for valid starting character.
1012 if (!eval_isnamec1(*arg))
1013 return arg;
1014
1015 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1016 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1017 // and can be used in slice "[n:]".
1018 if (*p == ':' && (p != arg + 1
1019 || !use_namespace
1020 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1021 break;
1022 return p;
1023}
1024
1025/*
1026 * Like to_name_end() but also skip over a list or dict constant.
1027 * Also accept "<SNR>123_Func".
1028 * This intentionally does not handle line continuation.
1029 */
1030 char_u *
1031to_name_const_end(char_u *arg)
1032{
1033 char_u *p = arg;
1034 typval_T rettv;
1035
1036 if (STRNCMP(p, "<SNR>", 5) == 0)
1037 p = skipdigits(p + 5);
1038 p = to_name_end(p, TRUE);
1039 if (p == arg && *arg == '[')
1040 {
1041
1042 // Can be "[1, 2, 3]->Func()".
1043 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1044 p = arg;
1045 }
1046 return p;
1047}
1048
1049/*
1050 * parse a list: [expr, expr]
1051 * "*arg" points to the '['.
1052 * ppconst->pp_is_const is set if all items are a constant.
1053 */
1054 static int
1055compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1056{
1057 char_u *p = skipwhite(*arg + 1);
1058 char_u *whitep = *arg + 1;
1059 int count = 0;
1060 int is_const;
1061 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001062 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001063
1064 for (;;)
1065 {
1066 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1067 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001068 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001069 return FAIL;
1070 }
1071 if (*p == ',')
1072 {
1073 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1074 return FAIL;
1075 }
1076 if (*p == ']')
1077 {
1078 ++p;
1079 break;
1080 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001081 if (must_end)
1082 {
1083 semsg(_(e_missing_comma_in_list_str), p);
1084 return FAIL;
1085 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001086 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1087 return FAIL;
1088 if (!is_const)
1089 is_all_const = FALSE;
1090 ++count;
1091 if (*p == ',')
1092 {
1093 ++p;
1094 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1095 {
1096 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1097 return FAIL;
1098 }
1099 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001100 else
1101 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001102 whitep = p;
1103 p = skipwhite(p);
1104 }
1105 *arg = p;
1106
1107 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001108 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001109}
1110
1111/*
1112 * Parse a lambda: "(arg, arg) => expr"
1113 * "*arg" points to the '('.
1114 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1115 */
1116 static int
1117compile_lambda(char_u **arg, cctx_T *cctx)
1118{
1119 int r;
1120 typval_T rettv;
1121 ufunc_T *ufunc;
1122 evalarg_T evalarg;
1123
1124 init_evalarg(&evalarg);
1125 evalarg.eval_flags = EVAL_EVALUATE;
1126 evalarg.eval_cctx = cctx;
1127
1128 // Get the funcref in "rettv".
1129 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1130 if (r != OK)
1131 {
1132 clear_evalarg(&evalarg, NULL);
1133 return r;
1134 }
1135
1136 // "rettv" will now be a partial referencing the function.
1137 ufunc = rettv.vval.v_partial->pt_func;
1138 ++ufunc->uf_refcount;
1139 clear_tv(&rettv);
1140
1141 // Compile it here to get the return type. The return type is optional,
1142 // when it's missing use t_unknown. This is recognized in
1143 // compile_return().
1144 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1145 ufunc->uf_ret_type = &t_unknown;
1146 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1147
1148 // When the outer function is compiled for profiling or debugging, the
1149 // lambda may be called without profiling or debugging. Compile it here in
1150 // the right context.
1151 if (cctx->ctx_compile_type == CT_DEBUG
1152#ifdef FEAT_PROFILE
1153 || cctx->ctx_compile_type == CT_PROFILE
1154#endif
1155 )
1156 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1157
Bram Moolenaar139575d2022-03-15 19:29:30 +00001158 // if the outer function is not compiled for debugging or profiling, this
1159 // one might be
1160 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001161 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001162 compiletype_T compile_type = get_compile_type(ufunc);
1163
1164 if (compile_type != CT_NONE)
1165 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001166 }
1167
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001168 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1169 // "*arg" may point into it. Point into the original line to avoid a
1170 // dangling pointer.
1171 if (evalarg.eval_using_cmdline)
1172 {
1173 garray_T *gap = &evalarg.eval_tofree_ga;
1174 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1175
1176 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1177 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001178 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001179 }
1180
1181 clear_evalarg(&evalarg, NULL);
1182
1183 if (ufunc->uf_def_status == UF_COMPILED)
1184 {
1185 // The return type will now be known.
1186 set_function_type(ufunc);
1187
1188 // The function reference count will be 1. When the ISN_FUNCREF
1189 // instruction is deleted the reference count is decremented and the
1190 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001191 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001192 }
1193
1194 func_ptr_unref(ufunc);
1195 return FAIL;
1196}
1197
1198/*
1199 * Get a lambda and compile it. Uses Vim9 syntax.
1200 */
1201 int
1202get_lambda_tv_and_compile(
1203 char_u **arg,
1204 typval_T *rettv,
1205 int types_optional,
1206 evalarg_T *evalarg)
1207{
1208 int r;
1209 ufunc_T *ufunc;
1210 int save_sc_version = current_sctx.sc_version;
1211
1212 // Get the funcref in "rettv".
1213 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1214 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1215 current_sctx.sc_version = save_sc_version;
1216 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001217 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001218
1219 // "rettv" will now be a partial referencing the function.
1220 ufunc = rettv->vval.v_partial->pt_func;
1221
1222 // Compile it here to get the return type. The return type is optional,
1223 // when it's missing use t_unknown. This is recognized in
1224 // compile_return().
1225 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1226 ufunc->uf_ret_type = &t_unknown;
1227 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1228
1229 if (ufunc->uf_def_status == UF_COMPILED)
1230 {
1231 // The return type will now be known.
1232 set_function_type(ufunc);
1233 return OK;
1234 }
1235 clear_tv(rettv);
1236 return FAIL;
1237}
1238
1239/*
1240 * parse a dict: {key: val, [key]: val}
1241 * "*arg" points to the '{'.
1242 * ppconst->pp_is_const is set if all item values are a constant.
1243 */
1244 static int
1245compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1246{
1247 garray_T *instr = &cctx->ctx_instr;
1248 int count = 0;
1249 dict_T *d = dict_alloc();
1250 dictitem_T *item;
1251 char_u *whitep = *arg + 1;
1252 char_u *p;
1253 int is_const;
1254 int is_all_const = TRUE; // reset when non-const encountered
1255
1256 if (d == NULL)
1257 return FAIL;
1258 if (generate_ppconst(cctx, ppconst) == FAIL)
1259 return FAIL;
1260 for (;;)
1261 {
1262 char_u *key = NULL;
1263
1264 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1265 {
1266 *arg = NULL;
1267 goto failret;
1268 }
1269
1270 if (**arg == '}')
1271 break;
1272
1273 if (**arg == '[')
1274 {
1275 isn_T *isn;
1276
1277 // {[expr]: value} uses an evaluated key.
1278 *arg = skipwhite(*arg + 1);
1279 if (compile_expr0(arg, cctx) == FAIL)
1280 return FAIL;
1281 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1282 if (isn->isn_type == ISN_PUSHNR)
1283 {
1284 char buf[NUMBUFLEN];
1285
1286 // Convert to string at compile time.
1287 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1288 isn->isn_type = ISN_PUSHS;
1289 isn->isn_arg.string = vim_strsave((char_u *)buf);
1290 }
1291 if (isn->isn_type == ISN_PUSHS)
1292 key = isn->isn_arg.string;
1293 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1294 return FAIL;
1295 *arg = skipwhite(*arg);
1296 if (**arg != ']')
1297 {
1298 emsg(_(e_missing_matching_bracket_after_dict_key));
1299 return FAIL;
1300 }
1301 ++*arg;
1302 }
1303 else
1304 {
1305 // {"name": value},
1306 // {'name': value},
1307 // {name: value} use "name" as a literal key
1308 key = get_literal_key(arg);
1309 if (key == NULL)
1310 return FAIL;
1311 if (generate_PUSHS(cctx, &key) == FAIL)
1312 return FAIL;
1313 }
1314
1315 // Check for duplicate keys, if using string keys.
1316 if (key != NULL)
1317 {
1318 item = dict_find(d, key, -1);
1319 if (item != NULL)
1320 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001321 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001322 goto failret;
1323 }
1324 item = dictitem_alloc(key);
1325 if (item != NULL)
1326 {
1327 item->di_tv.v_type = VAR_UNKNOWN;
1328 item->di_tv.v_lock = 0;
1329 if (dict_add(d, item) == FAIL)
1330 dictitem_free(item);
1331 }
1332 }
1333
1334 if (**arg != ':')
1335 {
1336 if (*skipwhite(*arg) == ':')
1337 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1338 else
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001339 semsg(_(e_missing_colon_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001340 return FAIL;
1341 }
1342 whitep = *arg + 1;
1343 if (!IS_WHITE_OR_NUL(*whitep))
1344 {
1345 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1346 return FAIL;
1347 }
1348
1349 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1350 {
1351 *arg = NULL;
1352 goto failret;
1353 }
1354
1355 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1356 return FAIL;
1357 if (!is_const)
1358 is_all_const = FALSE;
1359 ++count;
1360
1361 whitep = *arg;
1362 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1363 {
1364 *arg = NULL;
1365 goto failret;
1366 }
1367 if (**arg == '}')
1368 break;
1369 if (**arg != ',')
1370 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001371 semsg(_(e_missing_comma_in_dictionary_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001372 goto failret;
1373 }
1374 if (IS_WHITE_OR_NUL(*whitep))
1375 {
1376 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1377 return FAIL;
1378 }
1379 whitep = *arg + 1;
1380 if (!IS_WHITE_OR_NUL(*whitep))
1381 {
1382 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1383 return FAIL;
1384 }
1385 *arg = skipwhite(whitep);
1386 }
1387
1388 *arg = *arg + 1;
1389
1390 // Allow for following comment, after at least one space.
1391 p = skipwhite(*arg);
1392 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1393 *arg += STRLEN(*arg);
1394
1395 dict_unref(d);
1396 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001397 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001398
1399failret:
1400 if (*arg == NULL)
1401 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001402 semsg(_(e_missing_dict_end_str), _("[end of lines]"));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001403 *arg = (char_u *)"";
1404 }
1405 dict_unref(d);
1406 return FAIL;
1407}
1408
1409/*
1410 * Compile "&option".
1411 */
1412 static int
1413compile_get_option(char_u **arg, cctx_T *cctx)
1414{
1415 typval_T rettv;
1416 char_u *start = *arg;
1417 int ret;
1418
1419 // parse the option and get the current value to get the type.
1420 rettv.v_type = VAR_UNKNOWN;
1421 ret = eval_option(arg, &rettv, TRUE);
1422 if (ret == OK)
1423 {
1424 // include the '&' in the name, eval_option() expects it.
1425 char_u *name = vim_strnsave(start, *arg - start);
1426 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1427 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1428
1429 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1430 vim_free(name);
1431 }
1432 clear_tv(&rettv);
1433
1434 return ret;
1435}
1436
1437/*
1438 * Compile "$VAR".
1439 */
1440 static int
1441compile_get_env(char_u **arg, cctx_T *cctx)
1442{
1443 char_u *start = *arg;
1444 int len;
1445 int ret;
1446 char_u *name;
1447
1448 ++*arg;
1449 len = get_env_len(arg);
1450 if (len == 0)
1451 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001452 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001453 return FAIL;
1454 }
1455
1456 // include the '$' in the name, eval_env_var() expects it.
1457 name = vim_strnsave(start, len + 1);
1458 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1459 vim_free(name);
1460 return ret;
1461}
1462
1463/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001464 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001465 */
1466 static int
1467compile_interp_string(char_u **arg, cctx_T *cctx)
1468{
1469 typval_T tv;
1470 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001471 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001472 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001473 int count = 0;
1474 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001475
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001476 // *arg is on the '$' character, move it to the first string character.
1477 ++*arg;
1478 quote = **arg;
1479 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001480
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001481 for (;;)
1482 {
1483 // Get the string up to the matching quote or to a single '{'.
1484 // "arg" is advanced to either the quote or the '{'.
1485 if (quote == '"')
1486 ret = eval_string(arg, &tv, evaluate, TRUE);
1487 else
1488 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1489 if (ret == FAIL)
1490 break;
1491 if (evaluate)
1492 {
1493 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1494 || (**arg != '{' && count == 0))
1495 {
1496 // generate non-empty string or empty string if it's the only
1497 // one
1498 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1499 return FAIL;
1500 tv.vval.v_string = NULL; // don't free it now
1501 ++count;
1502 }
1503 clear_tv(&tv);
1504 }
1505
1506 if (**arg != '{')
1507 {
1508 // found terminating quote
1509 ++*arg;
1510 break;
1511 }
1512
1513 p = compile_one_expr_in_str(*arg, cctx);
1514 if (p == NULL)
1515 {
1516 ret = FAIL;
1517 break;
1518 }
1519 ++count;
1520 *arg = p;
1521 }
LemonBoy2eaef102022-05-06 13:14:50 +01001522
1523 if (ret == FAIL || !evaluate)
1524 return ret;
1525
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001526 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1527 if (count > 1)
1528 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001529
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001530 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001531}
1532
1533/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001534 * Compile "@r".
1535 */
1536 static int
1537compile_get_register(char_u **arg, cctx_T *cctx)
1538{
1539 int ret;
1540
1541 ++*arg;
1542 if (**arg == NUL)
1543 {
1544 semsg(_(e_syntax_error_at_str), *arg - 1);
1545 return FAIL;
1546 }
1547 if (!valid_yank_reg(**arg, FALSE))
1548 {
1549 emsg_invreg(**arg);
1550 return FAIL;
1551 }
1552 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1553 ++*arg;
1554 return ret;
1555}
1556
1557/*
1558 * Apply leading '!', '-' and '+' to constant "rettv".
1559 * When "numeric_only" is TRUE do not apply '!'.
1560 */
1561 static int
1562apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1563{
1564 char_u *p = *end;
1565
1566 // this works from end to start
1567 while (p > start)
1568 {
1569 --p;
1570 if (*p == '-' || *p == '+')
1571 {
1572 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001573 if (rettv->v_type == VAR_FLOAT)
1574 {
1575 if (*p == '-')
1576 rettv->vval.v_float = -rettv->vval.v_float;
1577 }
1578 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001579 {
1580 varnumber_T val;
1581 int error = FALSE;
1582
1583 // tv_get_number_chk() accepts a string, but we don't want that
1584 // here
1585 if (check_not_string(rettv) == FAIL)
1586 return FAIL;
1587 val = tv_get_number_chk(rettv, &error);
1588 clear_tv(rettv);
1589 if (error)
1590 return FAIL;
1591 if (*p == '-')
1592 val = -val;
1593 rettv->v_type = VAR_NUMBER;
1594 rettv->vval.v_number = val;
1595 }
1596 }
1597 else if (numeric_only)
1598 {
1599 ++p;
1600 break;
1601 }
1602 else if (*p == '!')
1603 {
1604 int v = tv2bool(rettv);
1605
1606 // '!' is permissive in the type.
1607 clear_tv(rettv);
1608 rettv->v_type = VAR_BOOL;
1609 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1610 }
1611 }
1612 *end = p;
1613 return OK;
1614}
1615
1616/*
1617 * Recognize v: variables that are constants and set "rettv".
1618 */
1619 static void
1620get_vim_constant(char_u **arg, typval_T *rettv)
1621{
1622 if (STRNCMP(*arg, "v:true", 6) == 0)
1623 {
1624 rettv->v_type = VAR_BOOL;
1625 rettv->vval.v_number = VVAL_TRUE;
1626 *arg += 6;
1627 }
1628 else if (STRNCMP(*arg, "v:false", 7) == 0)
1629 {
1630 rettv->v_type = VAR_BOOL;
1631 rettv->vval.v_number = VVAL_FALSE;
1632 *arg += 7;
1633 }
1634 else if (STRNCMP(*arg, "v:null", 6) == 0)
1635 {
1636 rettv->v_type = VAR_SPECIAL;
1637 rettv->vval.v_number = VVAL_NULL;
1638 *arg += 6;
1639 }
1640 else if (STRNCMP(*arg, "v:none", 6) == 0)
1641 {
1642 rettv->v_type = VAR_SPECIAL;
1643 rettv->vval.v_number = VVAL_NONE;
1644 *arg += 6;
1645 }
1646}
1647
1648 exprtype_T
1649get_compare_type(char_u *p, int *len, int *type_is)
1650{
1651 exprtype_T type = EXPR_UNKNOWN;
1652 int i;
1653
1654 switch (p[0])
1655 {
1656 case '=': if (p[1] == '=')
1657 type = EXPR_EQUAL;
1658 else if (p[1] == '~')
1659 type = EXPR_MATCH;
1660 break;
1661 case '!': if (p[1] == '=')
1662 type = EXPR_NEQUAL;
1663 else if (p[1] == '~')
1664 type = EXPR_NOMATCH;
1665 break;
1666 case '>': if (p[1] != '=')
1667 {
1668 type = EXPR_GREATER;
1669 *len = 1;
1670 }
1671 else
1672 type = EXPR_GEQUAL;
1673 break;
1674 case '<': if (p[1] != '=')
1675 {
1676 type = EXPR_SMALLER;
1677 *len = 1;
1678 }
1679 else
1680 type = EXPR_SEQUAL;
1681 break;
1682 case 'i': if (p[1] == 's')
1683 {
1684 // "is" and "isnot"; but not a prefix of a name
1685 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1686 *len = 5;
1687 i = p[*len];
1688 if (!isalnum(i) && i != '_')
1689 {
1690 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1691 *type_is = TRUE;
1692 }
1693 }
1694 break;
1695 }
1696 return type;
1697}
1698
1699/*
1700 * Skip over an expression, ignoring most errors.
1701 */
1702 void
1703skip_expr_cctx(char_u **arg, cctx_T *cctx)
1704{
1705 evalarg_T evalarg;
1706
1707 init_evalarg(&evalarg);
1708 evalarg.eval_cctx = cctx;
1709 skip_expr(arg, &evalarg);
1710 clear_evalarg(&evalarg, NULL);
1711}
1712
1713/*
1714 * Check that the top of the type stack has a type that can be used as a
1715 * condition. Give an error and return FAIL if not.
1716 */
1717 int
1718bool_on_stack(cctx_T *cctx)
1719{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001720 type_T *type;
1721
Bram Moolenaar078a4612022-01-04 15:17:03 +00001722 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001723 if (type == &t_bool)
1724 return OK;
1725
Bram Moolenaar4913d422022-10-17 13:13:32 +01001726 if (type->tt_type == VAR_ANY
1727 || type->tt_type == VAR_UNKNOWN
1728 || type->tt_type == VAR_NUMBER
1729 || type == &t_number_bool
1730 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001731 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1732 // This requires a runtime type check.
1733 return generate_COND2BOOL(cctx);
1734
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001735 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001736}
1737
1738/*
1739 * Give the "white on both sides" error, taking the operator from "p[len]".
1740 */
1741 void
1742error_white_both(char_u *op, int len)
1743{
1744 char_u buf[10];
1745
1746 vim_strncpy(buf, op, len);
1747 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1748}
1749
1750/*
1751 * Compile code to apply '-', '+' and '!'.
1752 * When "numeric_only" is TRUE do not apply '!'.
1753 */
1754 static int
1755compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1756{
1757 char_u *p = *end;
1758
1759 // this works from end to start
1760 while (p > start)
1761 {
1762 --p;
1763 while (VIM_ISWHITE(*p))
1764 --p;
1765 if (*p == '-' || *p == '+')
1766 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001767 type_T *type = get_type_on_stack(cctx, 0);
1768 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001769 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001770 return FAIL;
1771
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001772 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001773 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1774 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001775 }
1776 else if (numeric_only)
1777 {
1778 ++p;
1779 break;
1780 }
1781 else
1782 {
1783 int invert = *p == '!';
1784
1785 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1786 {
1787 if (p[-1] == '!')
1788 invert = !invert;
1789 --p;
1790 }
1791 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1792 return FAIL;
1793 }
1794 }
1795 *end = p;
1796 return OK;
1797}
1798
1799/*
1800 * Compile "(expression)": recursive!
1801 * Return FAIL/OK.
1802 */
1803 static int
1804compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1805{
1806 int ret;
1807 char_u *p = *arg + 1;
1808
1809 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1810 return FAIL;
1811 if (ppconst->pp_used <= PPSIZE - 10)
1812 {
1813 ret = compile_expr1(arg, cctx, ppconst);
1814 }
1815 else
1816 {
1817 // Not enough space in ppconst, flush constants.
1818 if (generate_ppconst(cctx, ppconst) == FAIL)
1819 return FAIL;
1820 ret = compile_expr0(arg, cctx);
1821 }
1822 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1823 return FAIL;
1824 if (**arg == ')')
1825 ++*arg;
1826 else if (ret == OK)
1827 {
1828 emsg(_(e_missing_closing_paren));
1829 ret = FAIL;
1830 }
1831 return ret;
1832}
1833
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001834static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001835
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001836/*
1837 * Compile whatever comes after "name" or "name()".
1838 * Advances "*arg" only when something was recognized.
1839 */
1840 static int
1841compile_subscript(
1842 char_u **arg,
1843 cctx_T *cctx,
1844 char_u *start_leader,
1845 char_u **end_leader,
1846 ppconst_T *ppconst)
1847{
1848 char_u *name_start = *end_leader;
1849 int keeping_dict = FALSE;
1850
1851 for (;;)
1852 {
1853 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001854 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001855
1856 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1857 {
1858 char_u *next = peek_next_line_from_context(cctx);
1859
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001860 // If a following line starts with "->{", "->(" or "->X" advance to
1861 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001862 // Also if a following line starts with ".x".
1863 if (next != NULL &&
1864 ((next[0] == '-' && next[1] == '>'
1865 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001866 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001867 || ASCII_ISALPHA(*skipwhite(next + 2))))
1868 || (next[0] == '.' && eval_isdictc(next[1]))))
1869 {
1870 next = next_line_from_context(cctx, TRUE);
1871 if (next == NULL)
1872 return FAIL;
1873 *arg = next;
1874 p = skipwhite(*arg);
1875 }
1876 }
1877
1878 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1879 // is not a function call.
1880 if (**arg == '(')
1881 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001882 int argcount = 0;
1883
1884 if (generate_ppconst(cctx, ppconst) == FAIL)
1885 return FAIL;
1886 ppconst->pp_is_const = FALSE;
1887
1888 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001889 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001890
1891 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001892 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001893 return FAIL;
1894 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1895 return FAIL;
1896 if (keeping_dict)
1897 {
1898 keeping_dict = FALSE;
1899 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1900 return FAIL;
1901 }
1902 }
1903 else if (*p == '-' && p[1] == '>')
1904 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001905 char_u *pstart = p;
1906 int alt;
1907 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001908
Bram Moolenaarc7349932022-01-16 20:59:39 +00001909 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001910 if (generate_ppconst(cctx, ppconst) == FAIL)
1911 return FAIL;
1912 ppconst->pp_is_const = FALSE;
1913
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001914 // Apply the '!', '-' and '+' first:
1915 // -1.0->func() works like (-1.0)->func()
1916 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1917 return FAIL;
1918
1919 p += 2;
1920 *arg = skipwhite(p);
1921 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001922
1923 // Three alternatives handled here:
1924 // 1. "base->name(" only a name, use compile_call()
1925 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1926 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001927 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001928 alt = 2;
1929 else
1930 {
1931 // alternative 1 or 3
1932 p = *arg;
1933 if (!eval_isnamec1(*p))
1934 {
1935 semsg(_(e_trailing_characters_str), pstart);
1936 return FAIL;
1937 }
1938 if (ASCII_ISALPHA(*p) && p[1] == ':')
1939 p += 2;
1940 for ( ; eval_isnamec(*p); ++p)
1941 ;
1942 if (*p == '(')
1943 {
1944 // alternative 1
1945 alt = 1;
1946 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1947 return FAIL;
1948 }
1949 else
1950 {
1951 // Must be alternative 3, find the "(". Only works within
1952 // one line.
1953 alt = 3;
1954 paren = vim_strchr(p, '(');
1955 if (paren == NULL)
1956 {
1957 semsg(_(e_missing_parenthesis_str), *arg);
1958 return FAIL;
1959 }
1960 }
1961 }
1962
1963 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001964 {
1965 int argcount = 1;
1966 garray_T *stack = &cctx->ctx_type_stack;
1967 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001968 int expr_isn_start = cctx->ctx_instr.ga_len;
1969 int expr_isn_end;
1970 int arg_isn_count;
1971
Bram Moolenaarc7349932022-01-16 20:59:39 +00001972 if (alt == 2)
1973 {
1974 // Funcref call: list->(Refs[2])(arg)
1975 // or lambda: list->((arg) => expr)(arg)
1976 //
1977 // Fist compile the function expression.
1978 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1979 return FAIL;
1980 }
1981 else
1982 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001983 int fail;
1984 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001985 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001986
Bram Moolenaarc7349932022-01-16 20:59:39 +00001987 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001988
1989 // instead of using LOADG for "import.Func" use PUSHFUNC
1990 ++paren_follows_after_expr;
1991
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001992 // do not look in the next line
1993 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001994
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001995 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001996 || *skipwhite(*arg) != NUL;
1997 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001998 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001999 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002000
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002001 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002002 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002003 if (did_emsg == prev_did_emsg)
2004 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002005 return FAIL;
2006 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002007 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002008
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002009 // Compile the arguments.
2010 if (**arg != '(')
2011 {
2012 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002013 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002014 else
2015 semsg(_(e_missing_parenthesis_str), *arg);
2016 return FAIL;
2017 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002018
2019 // Remember the next instruction index, where the instructions
2020 // for arguments are being written.
2021 expr_isn_end = cctx->ctx_instr.ga_len;
2022
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002023 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002024 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2025 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002026 return FAIL;
2027
2028 // Move the instructions for the arguments to before the
2029 // instructions of the expression and move the type of the
2030 // expression after the argument types. This is what ISN_PCALL
2031 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002032 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2033 if (arg_isn_count > 0)
2034 {
2035 int expr_isn_count = expr_isn_end - expr_isn_start;
2036 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002037 type_T *decl_type;
2038 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002039
2040 if (isn == NULL)
2041 return FAIL;
2042 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2043 + expr_isn_start,
2044 sizeof(isn_T) * expr_isn_count);
2045 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2046 + expr_isn_start,
2047 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2048 sizeof(isn_T) * arg_isn_count);
2049 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2050 + expr_isn_start + arg_isn_count,
2051 isn, sizeof(isn_T) * expr_isn_count);
2052 vim_free(isn);
2053
Bram Moolenaar078a4612022-01-04 15:17:03 +00002054 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2055 type = typep->type_curr;
2056 decl_type = typep->type_decl;
2057 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2058 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2059 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002060 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002061 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2062 typep->type_curr = type;
2063 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002064 }
2065
Bram Moolenaar078a4612022-01-04 15:17:03 +00002066 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002067 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2068 return FAIL;
2069 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002070
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002071 if (keeping_dict)
2072 {
2073 keeping_dict = FALSE;
2074 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2075 return FAIL;
2076 }
2077 }
2078 else if (**arg == '[')
2079 {
2080 int is_slice = FALSE;
2081
2082 // list index: list[123]
2083 // dict member: dict[key]
2084 // string index: text[123]
2085 // blob index: blob[123]
2086 if (generate_ppconst(cctx, ppconst) == FAIL)
2087 return FAIL;
2088 ppconst->pp_is_const = FALSE;
2089
2090 ++p;
2091 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2092 return FAIL;
2093 if (**arg == ':')
2094 {
2095 // missing first index is equal to zero
2096 generate_PUSHNR(cctx, 0);
2097 }
2098 else
2099 {
2100 if (compile_expr0(arg, cctx) == FAIL)
2101 return FAIL;
2102 if (**arg == ':')
2103 {
2104 semsg(_(e_white_space_required_before_and_after_str_at_str),
2105 ":", *arg);
2106 return FAIL;
2107 }
2108 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2109 return FAIL;
2110 *arg = skipwhite(*arg);
2111 }
2112 if (**arg == ':')
2113 {
2114 is_slice = TRUE;
2115 ++*arg;
2116 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2117 {
2118 semsg(_(e_white_space_required_before_and_after_str_at_str),
2119 ":", *arg);
2120 return FAIL;
2121 }
2122 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2123 return FAIL;
2124 if (**arg == ']')
2125 // missing second index is equal to end of string
2126 generate_PUSHNR(cctx, -1);
2127 else
2128 {
2129 if (compile_expr0(arg, cctx) == FAIL)
2130 return FAIL;
2131 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2132 return FAIL;
2133 *arg = skipwhite(*arg);
2134 }
2135 }
2136
2137 if (**arg != ']')
2138 {
2139 emsg(_(e_missing_closing_square_brace));
2140 return FAIL;
2141 }
2142 *arg = *arg + 1;
2143
2144 if (keeping_dict)
2145 {
2146 keeping_dict = FALSE;
2147 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2148 return FAIL;
2149 }
2150 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2151 return FAIL;
2152 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002153 else if (*p == '.'
2154 && (type = get_type_on_stack(cctx, 0)) != &t_unknown
2155 && (type->tt_type == VAR_CLASS || type->tt_type == VAR_OBJECT))
2156 {
2157 // class member: SomeClass.varname
2158 // class method: SomeClass.SomeMethod()
2159 // class constructor: SomeClass.new()
2160 // object member: someObject.varname, this.varname
2161 // object method: someObject.SomeMethod(), this.SomeMethod()
2162 if (compile_class_object_index(cctx, arg, type) == FAIL)
2163 return FAIL;
2164 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002165 else if (*p == '.' && p[1] != '.')
2166 {
2167 // dictionary member: dict.name
2168 if (generate_ppconst(cctx, ppconst) == FAIL)
2169 return FAIL;
2170 ppconst->pp_is_const = FALSE;
2171
2172 *arg = p + 1;
2173 if (IS_WHITE_OR_NUL(**arg))
2174 {
2175 emsg(_(e_missing_name_after_dot));
2176 return FAIL;
2177 }
2178 p = *arg;
2179 if (eval_isdictc(*p))
2180 while (eval_isnamec(*p))
2181 MB_PTR_ADV(p);
2182 if (p == *arg)
2183 {
2184 semsg(_(e_syntax_error_at_str), *arg);
2185 return FAIL;
2186 }
2187 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2188 return FAIL;
2189 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2190 return FAIL;
2191 keeping_dict = TRUE;
2192 *arg = p;
2193 }
2194 else
2195 break;
2196 }
2197
2198 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2199 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002200 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2201 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002202 return FAIL;
2203
2204 return OK;
2205}
2206
2207/*
2208 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2209 * "arg" is advanced until after the expression, skipping white space.
2210 *
2211 * If the value is a constant "ppconst->pp_used" will be non-zero.
2212 * Before instructions are generated, any values in "ppconst" will generated.
2213 *
2214 * This is the compiling equivalent of eval1(), eval2(), etc.
2215 */
2216
2217/*
2218 * number number constant
2219 * 0zFFFFFFFF Blob constant
2220 * "string" string constant
2221 * 'string' literal string constant
2222 * &option-name option value
2223 * @r register contents
2224 * identifier variable value
2225 * function() function call
2226 * $VAR environment variable
2227 * (expression) nested expression
2228 * [expr, expr] List
2229 * {key: val, [key]: val} Dictionary
2230 *
2231 * Also handle:
2232 * ! in front logical NOT
2233 * - in front unary minus
2234 * + in front unary plus (ignored)
2235 * trailing (arg) funcref/partial call
2236 * trailing [] subscript in String or List
2237 * trailing .name entry in Dictionary
2238 * trailing ->name() method call
2239 */
2240 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002241compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002242 char_u **arg,
2243 cctx_T *cctx,
2244 ppconst_T *ppconst)
2245{
2246 char_u *start_leader, *end_leader;
2247 int ret = OK;
2248 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2249 int used_before = ppconst->pp_used;
2250
2251 ppconst->pp_is_const = FALSE;
2252
2253 /*
2254 * Skip '!', '-' and '+' characters. They are handled later.
2255 */
2256 start_leader = *arg;
2257 if (eval_leader(arg, TRUE) == FAIL)
2258 return FAIL;
2259 end_leader = *arg;
2260
2261 rettv->v_type = VAR_UNKNOWN;
2262 switch (**arg)
2263 {
2264 /*
2265 * Number constant.
2266 */
2267 case '0': // also for blob starting with 0z
2268 case '1':
2269 case '2':
2270 case '3':
2271 case '4':
2272 case '5':
2273 case '6':
2274 case '7':
2275 case '8':
2276 case '9':
2277 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2278 return FAIL;
2279 // Apply "-" and "+" just before the number now, right to
2280 // left. Matters especially when "->" follows. Stops at
2281 // '!'.
2282 if (apply_leader(rettv, TRUE,
2283 start_leader, &end_leader) == FAIL)
2284 {
2285 clear_tv(rettv);
2286 return FAIL;
2287 }
2288 break;
2289
2290 /*
2291 * String constant: "string".
2292 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002293 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002294 return FAIL;
2295 break;
2296
2297 /*
2298 * Literal string constant: 'str''ing'.
2299 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002300 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002301 return FAIL;
2302 break;
2303
2304 /*
2305 * Constant Vim variable.
2306 */
2307 case 'v': get_vim_constant(arg, rettv);
2308 ret = NOTDONE;
2309 break;
2310
2311 /*
2312 * "true" constant
2313 */
2314 case 't': if (STRNCMP(*arg, "true", 4) == 0
2315 && !eval_isnamec((*arg)[4]))
2316 {
2317 *arg += 4;
2318 rettv->v_type = VAR_BOOL;
2319 rettv->vval.v_number = VVAL_TRUE;
2320 }
2321 else
2322 ret = NOTDONE;
2323 break;
2324
2325 /*
2326 * "false" constant
2327 */
2328 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2329 && !eval_isnamec((*arg)[5]))
2330 {
2331 *arg += 5;
2332 rettv->v_type = VAR_BOOL;
2333 rettv->vval.v_number = VVAL_FALSE;
2334 }
2335 else
2336 ret = NOTDONE;
2337 break;
2338
2339 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002340 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002341 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002342 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002343 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002344 char_u *p = *arg + 4;
2345 int len;
2346
2347 for (len = 0; eval_isnamec(p[len]); ++len)
2348 ;
2349 ret = handle_predefined(*arg, len + 4, rettv);
2350 if (ret == FAIL)
2351 ret = NOTDONE;
2352 else
2353 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002354 }
2355 else
2356 ret = NOTDONE;
2357 break;
2358
2359 /*
2360 * List: [expr, expr]
2361 */
2362 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2363 return FAIL;
2364 ret = compile_list(arg, cctx, ppconst);
2365 break;
2366
2367 /*
2368 * Dictionary: {'key': val, 'key': val}
2369 */
2370 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2371 return FAIL;
2372 ret = compile_dict(arg, cctx, ppconst);
2373 break;
2374
2375 /*
2376 * Option value: &name
2377 */
2378 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2379 return FAIL;
2380 ret = compile_get_option(arg, cctx);
2381 break;
2382
2383 /*
2384 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002385 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002386 */
2387 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2388 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002389 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2390 ret = compile_interp_string(arg, cctx);
2391 else
2392 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002393 break;
2394
2395 /*
2396 * Register contents: @r.
2397 */
2398 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2399 return FAIL;
2400 ret = compile_get_register(arg, cctx);
2401 break;
2402 /*
2403 * nested expression: (expression).
2404 * lambda: (arg, arg) => expr
2405 * funcref: (arg, arg) => { statement }
2406 */
2407 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2408 ret = compile_lambda(arg, cctx);
2409 if (ret == NOTDONE)
2410 ret = compile_parenthesis(arg, cctx, ppconst);
2411 break;
2412
2413 default: ret = NOTDONE;
2414 break;
2415 }
2416 if (ret == FAIL)
2417 return FAIL;
2418
2419 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2420 {
2421 if (cctx->ctx_skip == SKIP_YES)
2422 clear_tv(rettv);
2423 else
2424 // A constant expression can possibly be handled compile time,
2425 // return the value instead of generating code.
2426 ++ppconst->pp_used;
2427 }
2428 else if (ret == NOTDONE)
2429 {
2430 char_u *p;
2431 int r;
2432
2433 if (!eval_isnamec1(**arg))
2434 {
2435 if (!vim9_bad_comment(*arg))
2436 {
2437 if (ends_excmd(*skipwhite(*arg)))
2438 semsg(_(e_empty_expression_str), *arg);
2439 else
2440 semsg(_(e_name_expected_str), *arg);
2441 }
2442 return FAIL;
2443 }
2444
2445 // "name" or "name()"
2446 p = to_name_end(*arg, TRUE);
2447 if (p - *arg == (size_t)1 && **arg == '_')
2448 {
2449 emsg(_(e_cannot_use_underscore_here));
2450 return FAIL;
2451 }
2452
2453 if (*p == '(')
2454 {
2455 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2456 }
2457 else
2458 {
2459 if (cctx->ctx_skip != SKIP_YES
2460 && generate_ppconst(cctx, ppconst) == FAIL)
2461 return FAIL;
2462 r = compile_load(arg, p, cctx, TRUE, TRUE);
2463 }
2464 if (r == FAIL)
2465 return FAIL;
2466 }
2467
2468 // Handle following "[]", ".member", etc.
2469 // Then deal with prefixed '-', '+' and '!', if not done already.
2470 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2471 ppconst) == FAIL)
2472 return FAIL;
2473 if (ppconst->pp_used > 0)
2474 {
2475 // apply the '!', '-' and '+' before the constant
2476 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2477 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2478 return FAIL;
2479 return OK;
2480 }
2481 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2482 return FAIL;
2483 return OK;
2484}
2485
2486/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002487 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002488 */
2489 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002490compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002491{
2492 type_T *want_type = NULL;
2493
2494 // Recognize <type>
2495 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2496 {
2497 ++*arg;
2498 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2499 if (want_type == NULL)
2500 return FAIL;
2501
2502 if (**arg != '>')
2503 {
2504 if (*skipwhite(*arg) == '>')
2505 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2506 else
2507 emsg(_(e_missing_gt));
2508 return FAIL;
2509 }
2510 ++*arg;
2511 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2512 return FAIL;
2513 }
2514
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002515 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002516 return FAIL;
2517
2518 if (want_type != NULL)
2519 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002520 type_T *actual;
2521 where_T where = WHERE_INIT;
2522
2523 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002524 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002525 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002526 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002527 if (need_type(actual, want_type, FALSE,
2528 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002529 return FAIL;
2530 }
2531 }
2532
2533 return OK;
2534}
2535
2536/*
2537 * * number multiplication
2538 * / number division
2539 * % number modulo
2540 */
2541 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002542compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002543{
2544 char_u *op;
2545 char_u *next;
2546 int ppconst_used = ppconst->pp_used;
2547
2548 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002549 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002550 return FAIL;
2551
2552 /*
2553 * Repeat computing, until no "*", "/" or "%" is following.
2554 */
2555 for (;;)
2556 {
2557 op = may_peek_next_line(cctx, *arg, &next);
2558 if (*op != '*' && *op != '/' && *op != '%')
2559 break;
2560 if (next != NULL)
2561 {
2562 *arg = next_line_from_context(cctx, TRUE);
2563 op = skipwhite(*arg);
2564 }
2565
2566 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2567 {
2568 error_white_both(op, 1);
2569 return FAIL;
2570 }
2571 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2572 return FAIL;
2573
2574 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002575 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002576 return FAIL;
2577
2578 if (ppconst->pp_used == ppconst_used + 2
2579 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2580 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2581 {
2582 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2583 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2584 varnumber_T res = 0;
2585 int failed = FALSE;
2586
2587 // both are numbers: compute the result
2588 switch (*op)
2589 {
2590 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2591 break;
2592 case '/': res = num_divide(tv1->vval.v_number,
2593 tv2->vval.v_number, &failed);
2594 break;
2595 case '%': res = num_modulus(tv1->vval.v_number,
2596 tv2->vval.v_number, &failed);
2597 break;
2598 }
2599 if (failed)
2600 return FAIL;
2601 tv1->vval.v_number = res;
2602 --ppconst->pp_used;
2603 }
2604 else
2605 {
2606 generate_ppconst(cctx, ppconst);
2607 generate_two_op(cctx, op);
2608 }
2609 }
2610
2611 return OK;
2612}
2613
2614/*
2615 * + number addition or list/blobl concatenation
2616 * - number subtraction
2617 * .. string concatenation
2618 */
2619 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002620compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002621{
2622 char_u *op;
2623 char_u *next;
2624 int oplen;
2625 int ppconst_used = ppconst->pp_used;
2626
2627 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002628 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002629 return FAIL;
2630
2631 /*
2632 * Repeat computing, until no "+", "-" or ".." is following.
2633 */
2634 for (;;)
2635 {
2636 op = may_peek_next_line(cctx, *arg, &next);
2637 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2638 break;
2639 if (op[0] == op[1] && *op != '.' && next)
2640 // Finding "++" or "--" on the next line is a separate command.
2641 // But ".." is concatenation.
2642 break;
2643 oplen = (*op == '.' ? 2 : 1);
2644 if (next != NULL)
2645 {
2646 *arg = next_line_from_context(cctx, TRUE);
2647 op = skipwhite(*arg);
2648 }
2649
2650 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2651 {
2652 error_white_both(op, oplen);
2653 return FAIL;
2654 }
2655
2656 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2657 return FAIL;
2658
2659 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002660 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002661 return FAIL;
2662
2663 if (ppconst->pp_used == ppconst_used + 2
2664 && (*op == '.'
2665 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2666 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2667 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2668 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2669 {
2670 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2671 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2672
2673 // concat/subtract/add constant numbers
2674 if (*op == '+')
2675 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2676 else if (*op == '-')
2677 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2678 else
2679 {
2680 // concatenate constant strings
2681 char_u *s1 = tv1->vval.v_string;
2682 char_u *s2 = tv2->vval.v_string;
2683 size_t len1 = STRLEN(s1);
2684
2685 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2686 if (tv1->vval.v_string == NULL)
2687 {
2688 clear_ppconst(ppconst);
2689 return FAIL;
2690 }
2691 mch_memmove(tv1->vval.v_string, s1, len1);
2692 STRCPY(tv1->vval.v_string + len1, s2);
2693 vim_free(s1);
2694 vim_free(s2);
2695 }
2696 --ppconst->pp_used;
2697 }
2698 else
2699 {
2700 generate_ppconst(cctx, ppconst);
2701 ppconst->pp_is_const = FALSE;
2702 if (*op == '.')
2703 {
2704 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2705 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2706 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002707 if (generate_CONCAT(cctx, 2) == FAIL)
2708 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002709 }
2710 else
2711 generate_two_op(cctx, op);
2712 }
2713 }
2714
2715 return OK;
2716}
2717
2718/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002719 * expr6a >> expr6b
2720 * expr6a << expr6b
2721 *
2722 * Produces instructions:
2723 * OPNR bitwise left or right shift
2724 */
2725 static int
2726compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2727{
2728 exprtype_T type = EXPR_UNKNOWN;
2729 char_u *p;
2730 char_u *next;
2731 int len = 2;
2732 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002733 isn_T *isn;
2734
2735 // get the first variable
2736 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2737 return FAIL;
2738
2739 /*
2740 * Repeat computing, until no "+", "-" or ".." is following.
2741 */
2742 for (;;)
2743 {
2744 type = EXPR_UNKNOWN;
2745
2746 p = may_peek_next_line(cctx, *arg, &next);
2747 if (p[0] == '<' && p[1] == '<')
2748 type = EXPR_LSHIFT;
2749 else if (p[0] == '>' && p[1] == '>')
2750 type = EXPR_RSHIFT;
2751
2752 if (type == EXPR_UNKNOWN)
2753 return OK;
2754
2755 // Handle a bitwise left or right shift operator
2756 if (ppconst->pp_used == ppconst_used + 1)
2757 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002758 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002759 {
2760 // left operand should be a number
2761 emsg(_(e_bitshift_ops_must_be_number));
2762 return FAIL;
2763 }
2764 }
2765 else
2766 {
2767 type_T *t = get_type_on_stack(cctx, 0);
2768
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002769 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002770 {
2771 emsg(_(e_bitshift_ops_must_be_number));
2772 return FAIL;
2773 }
2774 }
2775
2776 if (next != NULL)
2777 {
2778 *arg = next_line_from_context(cctx, TRUE);
2779 p = skipwhite(*arg);
2780 }
2781
2782 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2783 {
2784 error_white_both(p, len);
2785 return FAIL;
2786 }
2787
2788 // get the second variable
2789 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2790 return FAIL;
2791
2792 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2793 return FAIL;
2794
2795 if (ppconst->pp_used == ppconst_used + 2)
2796 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002797 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2798 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2799
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002800 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002801 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2802 {
2803 // right operand should be a positive number
2804 if (tv2->v_type != VAR_NUMBER)
2805 emsg(_(e_bitshift_ops_must_be_number));
2806 else
dundargocc57b5bc2022-11-02 13:30:51 +00002807 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002808 return FAIL;
2809 }
2810
2811 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2812 tv1->vval.v_number = 0;
2813 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002814 tv1->vval.v_number =
2815 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002816 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002817 tv1->vval.v_number =
2818 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002819 clear_tv(tv2);
2820 --ppconst->pp_used;
2821 }
2822 else
2823 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002824 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2825 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002826 {
2827 emsg(_(e_bitshift_ops_must_be_number));
2828 return FAIL;
2829 }
2830
2831 generate_ppconst(cctx, ppconst);
2832
2833 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2834 if (isn == NULL)
2835 return FAIL;
2836
2837 if (isn != NULL)
2838 isn->isn_arg.op.op_type = type;
2839 }
2840 }
2841
2842 return OK;
2843}
2844
2845/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002846 * expr5a == expr5b
2847 * expr5a =~ expr5b
2848 * expr5a != expr5b
2849 * expr5a !~ expr5b
2850 * expr5a > expr5b
2851 * expr5a >= expr5b
2852 * expr5a < expr5b
2853 * expr5a <= expr5b
2854 * expr5a is expr5b
2855 * expr5a isnot expr5b
2856 *
2857 * Produces instructions:
2858 * EVAL expr5a Push result of "expr5a"
2859 * EVAL expr5b Push result of "expr5b"
2860 * COMPARE one of the compare instructions
2861 */
2862 static int
2863compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2864{
2865 exprtype_T type = EXPR_UNKNOWN;
2866 char_u *p;
2867 char_u *next;
2868 int len = 2;
2869 int type_is = FALSE;
2870 int ppconst_used = ppconst->pp_used;
2871
2872 // get the first variable
2873 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2874 return FAIL;
2875
2876 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002877
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002878 type = get_compare_type(p, &len, &type_is);
2879
2880 /*
2881 * If there is a comparative operator, use it.
2882 */
2883 if (type != EXPR_UNKNOWN)
2884 {
2885 int ic = FALSE; // Default: do not ignore case
2886
2887 if (next != NULL)
2888 {
2889 *arg = next_line_from_context(cctx, TRUE);
2890 p = skipwhite(*arg);
2891 }
2892 if (type_is && (p[len] == '?' || p[len] == '#'))
2893 {
2894 semsg(_(e_invalid_expression_str), *arg);
2895 return FAIL;
2896 }
2897 // extra question mark appended: ignore case
2898 if (p[len] == '?')
2899 {
2900 ic = TRUE;
2901 ++len;
2902 }
2903 // extra '#' appended: match case (ignored)
2904 else if (p[len] == '#')
2905 ++len;
2906 // nothing appended: match case
2907
2908 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2909 {
2910 error_white_both(p, len);
2911 return FAIL;
2912 }
2913
2914 // get the second variable
2915 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2916 return FAIL;
2917
2918 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2919 return FAIL;
2920
2921 if (ppconst->pp_used == ppconst_used + 2)
2922 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002923 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002924 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2925 int ret;
2926
2927 // Both sides are a constant, compute the result now.
2928 // First check for a valid combination of types, this is more
2929 // strict than typval_compare().
2930 if (check_compare_types(type, tv1, tv2) == FAIL)
2931 ret = FAIL;
2932 else
2933 {
2934 ret = typval_compare(tv1, tv2, type, ic);
2935 tv1->v_type = VAR_BOOL;
2936 tv1->vval.v_number = tv1->vval.v_number
2937 ? VVAL_TRUE : VVAL_FALSE;
2938 clear_tv(tv2);
2939 --ppconst->pp_used;
2940 }
2941 return ret;
2942 }
2943
2944 generate_ppconst(cctx, ppconst);
2945 return generate_COMPARE(cctx, type, ic);
2946 }
2947
2948 return OK;
2949}
2950
2951static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2952
2953/*
2954 * Compile || or &&.
2955 */
2956 static int
2957compile_and_or(
2958 char_u **arg,
2959 cctx_T *cctx,
2960 char *op,
2961 ppconst_T *ppconst,
2962 int ppconst_used UNUSED)
2963{
2964 char_u *next;
2965 char_u *p = may_peek_next_line(cctx, *arg, &next);
2966 int opchar = *op;
2967
2968 if (p[0] == opchar && p[1] == opchar)
2969 {
2970 garray_T *instr = &cctx->ctx_instr;
2971 garray_T end_ga;
2972 int save_skip = cctx->ctx_skip;
2973
2974 /*
2975 * Repeat until there is no following "||" or "&&"
2976 */
2977 ga_init2(&end_ga, sizeof(int), 10);
2978 while (p[0] == opchar && p[1] == opchar)
2979 {
2980 long start_lnum = SOURCING_LNUM;
2981 long save_sourcing_lnum;
2982 int start_ctx_lnum = cctx->ctx_lnum;
2983 int save_lnum;
2984 int const_used;
2985 int status;
2986 jumpwhen_T jump_when = opchar == '|'
2987 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2988
2989 if (next != NULL)
2990 {
2991 *arg = next_line_from_context(cctx, TRUE);
2992 p = skipwhite(*arg);
2993 }
2994
2995 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2996 {
2997 semsg(_(e_white_space_required_before_and_after_str_at_str),
2998 op, p);
2999 ga_clear(&end_ga);
3000 return FAIL;
3001 }
3002
3003 save_sourcing_lnum = SOURCING_LNUM;
3004 SOURCING_LNUM = start_lnum;
3005 save_lnum = cctx->ctx_lnum;
3006 cctx->ctx_lnum = start_ctx_lnum;
3007
3008 status = check_ppconst_bool(ppconst);
3009 if (status != FAIL)
3010 {
3011 // Use the last ppconst if possible.
3012 if (ppconst->pp_used > 0)
3013 {
3014 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3015 int is_true = tv2bool(tv);
3016
3017 if ((is_true && opchar == '|')
3018 || (!is_true && opchar == '&'))
3019 {
3020 // For "false && expr" and "true || expr" the "expr"
3021 // does not need to be evaluated.
3022 cctx->ctx_skip = SKIP_YES;
3023 clear_tv(tv);
3024 tv->v_type = VAR_BOOL;
3025 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3026 }
3027 else
3028 {
3029 // For "true && expr" and "false || expr" only "expr"
3030 // needs to be evaluated.
3031 --ppconst->pp_used;
3032 jump_when = JUMP_NEVER;
3033 }
3034 }
3035 else
3036 {
3037 // Every part must evaluate to a bool.
3038 status = bool_on_stack(cctx);
3039 }
3040 }
3041 if (status != FAIL)
3042 status = ga_grow(&end_ga, 1);
3043 cctx->ctx_lnum = save_lnum;
3044 if (status == FAIL)
3045 {
3046 ga_clear(&end_ga);
3047 return FAIL;
3048 }
3049
3050 if (jump_when != JUMP_NEVER)
3051 {
3052 if (cctx->ctx_skip != SKIP_YES)
3053 {
3054 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3055 ++end_ga.ga_len;
3056 }
3057 generate_JUMP(cctx, jump_when, 0);
3058 }
3059
3060 // eval the next expression
3061 SOURCING_LNUM = save_sourcing_lnum;
3062 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3063 {
3064 ga_clear(&end_ga);
3065 return FAIL;
3066 }
3067
3068 const_used = ppconst->pp_used;
3069 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3070 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3071 {
3072 ga_clear(&end_ga);
3073 return FAIL;
3074 }
3075
3076 // "0 || 1" results in true, "1 && 0" results in false.
3077 if (ppconst->pp_used == const_used + 1)
3078 {
3079 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3080
3081 if (tv->v_type == VAR_NUMBER
3082 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3083 {
3084 tv->vval.v_number = tv->vval.v_number == 1
3085 ? VVAL_TRUE : VVAL_FALSE;
3086 tv->v_type = VAR_BOOL;
3087 }
3088 }
3089
3090 p = may_peek_next_line(cctx, *arg, &next);
3091 }
3092
3093 if (check_ppconst_bool(ppconst) == FAIL)
3094 {
3095 ga_clear(&end_ga);
3096 return FAIL;
3097 }
3098
3099 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3100 // Every part must evaluate to a bool.
3101 if (bool_on_stack(cctx) == FAIL)
3102 {
3103 ga_clear(&end_ga);
3104 return FAIL;
3105 }
3106
3107 if (end_ga.ga_len > 0)
3108 {
3109 // Fill in the end label in all jumps.
3110 generate_ppconst(cctx, ppconst);
3111 while (end_ga.ga_len > 0)
3112 {
3113 isn_T *isn;
3114
3115 --end_ga.ga_len;
3116 isn = ((isn_T *)instr->ga_data)
3117 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3118 isn->isn_arg.jump.jump_where = instr->ga_len;
3119 }
3120 }
3121 ga_clear(&end_ga);
3122
3123 cctx->ctx_skip = save_skip;
3124 }
3125
3126 return OK;
3127}
3128
3129/*
3130 * expr4a && expr4a && expr4a logical AND
3131 *
3132 * Produces instructions:
3133 * EVAL expr4a Push result of "expr4a"
3134 * COND2BOOL convert to bool if needed
3135 * JUMP_IF_COND_FALSE end
3136 * EVAL expr4b Push result of "expr4b"
3137 * JUMP_IF_COND_FALSE end
3138 * EVAL expr4c Push result of "expr4c"
3139 * end:
3140 */
3141 static int
3142compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3143{
3144 int ppconst_used = ppconst->pp_used;
3145
3146 // get the first variable
3147 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3148 return FAIL;
3149
3150 // || and && work almost the same
3151 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3152}
3153
3154/*
3155 * expr3a || expr3b || expr3c logical OR
3156 *
3157 * Produces instructions:
3158 * EVAL expr3a Push result of "expr3a"
3159 * COND2BOOL convert to bool if needed
3160 * JUMP_IF_COND_TRUE end
3161 * EVAL expr3b Push result of "expr3b"
3162 * JUMP_IF_COND_TRUE end
3163 * EVAL expr3c Push result of "expr3c"
3164 * end:
3165 */
3166 static int
3167compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3168{
3169 int ppconst_used = ppconst->pp_used;
3170
3171 // eval the first expression
3172 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3173 return FAIL;
3174
3175 // || and && work almost the same
3176 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3177}
3178
3179/*
3180 * Toplevel expression: expr2 ? expr1a : expr1b
3181 * Produces instructions:
3182 * EVAL expr2 Push result of "expr2"
3183 * JUMP_IF_FALSE alt jump if false
3184 * EVAL expr1a
3185 * JUMP_ALWAYS end
3186 * alt: EVAL expr1b
3187 * end:
3188 *
3189 * Toplevel expression: expr2 ?? expr1
3190 * Produces instructions:
3191 * EVAL expr2 Push result of "expr2"
3192 * JUMP_AND_KEEP_IF_TRUE end jump if true
3193 * EVAL expr1
3194 * end:
3195 */
3196 int
3197compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3198{
3199 char_u *p;
3200 int ppconst_used = ppconst->pp_used;
3201 char_u *next;
3202
3203 // Ignore all kinds of errors when not producing code.
3204 if (cctx->ctx_skip == SKIP_YES)
3205 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003206 int prev_did_emsg = did_emsg;
3207
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003208 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003209 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003210 }
3211
3212 // Evaluate the first expression.
3213 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3214 return FAIL;
3215
3216 p = may_peek_next_line(cctx, *arg, &next);
3217 if (*p == '?')
3218 {
3219 int op_falsy = p[1] == '?';
3220 garray_T *instr = &cctx->ctx_instr;
3221 garray_T *stack = &cctx->ctx_type_stack;
3222 int alt_idx = instr->ga_len;
3223 int end_idx = 0;
3224 isn_T *isn;
3225 type_T *type1 = NULL;
3226 int has_const_expr = FALSE;
3227 int const_value = FALSE;
3228 int save_skip = cctx->ctx_skip;
3229
3230 if (next != NULL)
3231 {
3232 *arg = next_line_from_context(cctx, TRUE);
3233 p = skipwhite(*arg);
3234 }
3235
3236 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3237 {
3238 semsg(_(e_white_space_required_before_and_after_str_at_str),
3239 op_falsy ? "??" : "?", p);
3240 return FAIL;
3241 }
3242
3243 if (ppconst->pp_used == ppconst_used + 1)
3244 {
3245 // the condition is a constant, we know whether the ? or the :
3246 // expression is to be evaluated.
3247 has_const_expr = TRUE;
3248 if (op_falsy)
3249 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3250 else
3251 {
3252 int error = FALSE;
3253
3254 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3255 &error);
3256 if (error)
3257 return FAIL;
3258 }
3259 cctx->ctx_skip = save_skip == SKIP_YES ||
3260 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3261
3262 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3263 // "left ?? right" and "left" is truthy: produce "left"
3264 generate_ppconst(cctx, ppconst);
3265 else
3266 {
3267 clear_tv(&ppconst->pp_tv[ppconst_used]);
3268 --ppconst->pp_used;
3269 }
3270 }
3271 else
3272 {
3273 generate_ppconst(cctx, ppconst);
3274 if (op_falsy)
3275 end_idx = instr->ga_len;
3276 generate_JUMP(cctx, op_falsy
3277 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3278 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003279 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003280 }
3281
3282 // evaluate the second expression; any type is accepted
3283 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3284 return FAIL;
3285 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3286 return FAIL;
3287
3288 if (!has_const_expr)
3289 {
3290 generate_ppconst(cctx, ppconst);
3291
3292 if (!op_falsy)
3293 {
3294 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003295 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003296 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003297
3298 end_idx = instr->ga_len;
3299 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3300
3301 // jump here from JUMP_IF_FALSE
3302 isn = ((isn_T *)instr->ga_data) + alt_idx;
3303 isn->isn_arg.jump.jump_where = instr->ga_len;
3304 }
3305 }
3306
3307 if (!op_falsy)
3308 {
3309 // Check for the ":".
3310 p = may_peek_next_line(cctx, *arg, &next);
3311 if (*p != ':')
3312 {
3313 emsg(_(e_missing_colon_after_questionmark));
3314 return FAIL;
3315 }
3316 if (next != NULL)
3317 {
3318 *arg = next_line_from_context(cctx, TRUE);
3319 p = skipwhite(*arg);
3320 }
3321
3322 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3323 {
3324 semsg(_(e_white_space_required_before_and_after_str_at_str),
3325 ":", p);
3326 return FAIL;
3327 }
3328
3329 // evaluate the third expression
3330 if (has_const_expr)
3331 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3332 ? SKIP_YES : SKIP_NOT;
3333 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3334 return FAIL;
3335 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3336 return FAIL;
3337 }
3338
3339 if (!has_const_expr)
3340 {
3341 type_T **typep;
3342
3343 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003344 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003345
3346 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003347 typep = &((((type2_T *)stack->ga_data)
3348 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003349 common_type(type1, *typep, typep, cctx->ctx_type_list);
3350
3351 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3352 isn = ((isn_T *)instr->ga_data) + end_idx;
3353 isn->isn_arg.jump.jump_where = instr->ga_len;
3354 }
3355
3356 cctx->ctx_skip = save_skip;
3357 }
3358 return OK;
3359}
3360
3361/*
3362 * Toplevel expression.
3363 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3364 * Returns OK or FAIL.
3365 */
3366 int
3367compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3368{
3369 ppconst_T ppconst;
3370
3371 CLEAR_FIELD(ppconst);
3372 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3373 {
3374 clear_ppconst(&ppconst);
3375 return FAIL;
3376 }
3377 if (is_const != NULL)
3378 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3379 if (generate_ppconst(cctx, &ppconst) == FAIL)
3380 return FAIL;
3381 return OK;
3382}
3383
3384/*
3385 * Toplevel expression.
3386 */
3387 int
3388compile_expr0(char_u **arg, cctx_T *cctx)
3389{
3390 return compile_expr0_ext(arg, cctx, NULL);
3391}
3392
3393
3394#endif // defined(FEAT_EVAL)