blob: ba2c826cff03489035ea3a4bde6fbd0f00f786d6 [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 {
276 // TODO
277 }
278 else if (type->tt_type == VAR_OBJECT)
279 {
280 for (int i = 0; i < cl->class_obj_member_count; ++i)
281 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000282 ocmember_T *m = &cl->class_obj_members[i];
283 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000284 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000285 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
286 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000287 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000288 return FAIL;
289 }
290
Bram Moolenaard505d172022-12-18 21:42:55 +0000291 generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000292
293 *arg = name_end;
294 return OK;
295 }
296 }
297
298 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
299 }
300 else
301 {
302 // TODO: class member
303 emsg("compile_class_object_index(): not handled");
304 }
305
306 return FAIL;
307}
308
309/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000310 * Generate an instruction to load script-local variable "name", without the
311 * leading "s:".
312 * Also finds imported variables.
313 */
314 int
315compile_load_scriptvar(
316 cctx_T *cctx,
317 char_u *name, // variable NUL terminated
318 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100319 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000320{
321 scriptitem_T *si;
322 int idx;
323 imported_T *import;
324
325 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
326 return FAIL;
327 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000328 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000329 if (idx >= 0)
330 {
331 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
332
333 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
334 current_sctx.sc_sid, idx, sv->sv_type);
335 return OK;
336 }
337
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000338 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000339 if (import != NULL)
340 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000341 char_u *p = skipwhite(*end);
342 char_u *exp_name;
343 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100344 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000345 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000346 int done = FALSE;
347 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000348
349 // Need to lookup the member.
350 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000351 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000352 semsg(_(e_expected_dot_after_name_str), start);
353 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000354 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000355 ++p;
356 if (VIM_ISWHITE(*p))
357 {
358 emsg(_(e_no_white_space_allowed_after_dot));
359 return FAIL;
360 }
361
362 // isolate one name
363 exp_name = p;
364 while (eval_isnamec(*p))
365 ++p;
366 cc = *p;
367 *p = NUL;
368
Bram Moolenaard041f422022-01-12 19:54:00 +0000369 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100370 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
371 // "import autoload './dir/script.vim'" or
372 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100373 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100374
375 if (res == OK)
376 {
377 if (si->sn_autoload_prefix != NULL
378 && si->sn_state == SN_STATE_NOT_LOADED)
379 {
380 char_u *auto_name =
381 concat_str(si->sn_autoload_prefix, exp_name);
382
383 // autoload script must be loaded later, access by the autoload
384 // name. If a '(' follows it must be a function. Otherwise we
385 // don't know, it can be "script.Func".
386 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100387 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100388 else
389 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
390 vim_free(auto_name);
391 done = TRUE;
392 }
393 else if (si->sn_import_autoload
394 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100395 {
396 // If a '(' follows it must be a function. Otherwise we don't
397 // know, it can be "script.Func".
398 if (cc == '(' || paren_follows_after_expr)
399 {
400 char_u sid_name[MAX_FUNC_NAME_LEN];
401
402 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100403 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100404 }
405 else
406 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
407 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100408 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100409 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100410 else
411 {
412 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
413 cctx, NULL, TRUE);
414 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100415 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100416
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000417 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000418 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000419 if (done)
420 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000421
422 if (idx < 0)
423 {
424 if (ufunc != NULL)
425 {
426 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100427 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000428 return OK;
429 }
430 return FAIL;
431 }
432
433 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
434 import->imp_sid,
435 idx,
436 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000437 return OK;
438 }
439
Bram Moolenaard0132f42022-05-12 11:05:40 +0100440 // Can only get here if we know "name" is a script variable and not in a
441 // Vim9 script (variable is not in sn_var_vals): old style script.
442 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000443 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000444}
445
446 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000447generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000448{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000449 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000450 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000451
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000452 // Reject a global non-autoload function found without the "g:" prefix.
453 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000454 return FAIL;
455
456 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000457 compile_type = get_compile_type(ufunc);
458 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000459 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000460 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100461 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000462}
463
464/*
465 * Compile a variable name into a load instruction.
466 * "end" points to just after the name.
467 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
468 * When "error" is FALSE do not give an error when not found.
469 */
470 int
471compile_load(
472 char_u **arg,
473 char_u *end_arg,
474 cctx_T *cctx,
475 int is_expr,
476 int error)
477{
478 type_T *type;
479 char_u *name = NULL;
480 char_u *end = end_arg;
481 int res = FAIL;
482 int prev_called_emsg = called_emsg;
483
484 if (*(*arg + 1) == ':')
485 {
486 if (end <= *arg + 2)
487 {
488 isntype_T isn_type;
489
490 // load dictionary of namespace
491 switch (**arg)
492 {
493 case 'g': isn_type = ISN_LOADGDICT; break;
494 case 'w': isn_type = ISN_LOADWDICT; break;
495 case 't': isn_type = ISN_LOADTDICT; break;
496 case 'b': isn_type = ISN_LOADBDICT; break;
497 default:
498 semsg(_(e_namespace_not_supported_str), *arg);
499 goto theend;
500 }
501 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
502 goto theend;
503 res = OK;
504 }
505 else
506 {
507 isntype_T isn_type = ISN_DROP;
508
509 // load namespaced variable
510 name = vim_strnsave(*arg + 2, end - (*arg + 2));
511 if (name == NULL)
512 return FAIL;
513
514 switch (**arg)
515 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100516 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000517 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000518 case 's': if (current_script_is_vim9())
519 {
520 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
521 *arg);
522 vim_free(name);
523 return FAIL;
524 }
Kota Kato948a3892022-08-16 16:09:59 +0100525 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000526 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000527 else
528 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100529 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000530 break;
531 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
532 {
533 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000534 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000535 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000536 else
537 isn_type = ISN_LOADG;
538 }
539 else
540 {
541 isn_type = ISN_LOADAUTO;
542 vim_free(name);
543 name = vim_strnsave(*arg, end - *arg);
544 if (name == NULL)
545 return FAIL;
546 }
547 break;
548 case 'w': isn_type = ISN_LOADW; break;
549 case 't': isn_type = ISN_LOADT; break;
550 case 'b': isn_type = ISN_LOADB; break;
551 default: // cannot happen, just in case
552 semsg(_(e_namespace_not_supported_str), *arg);
553 goto theend;
554 }
555 if (isn_type != ISN_DROP)
556 {
557 // Global, Buffer-local, Window-local and Tabpage-local
558 // variables can be defined later, thus we don't check if it
559 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000560 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000561 }
562 }
563 }
564 else
565 {
566 size_t len = end - *arg;
567 int idx;
568 int gen_load = FALSE;
569 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100570 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100571 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000572
573 name = vim_strnsave(*arg, end - *arg);
574 if (name == NULL)
575 return FAIL;
576
577 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
578 {
579 script_autoload(name, FALSE);
580 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
581 }
582 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
583 == OK)
584 {
585 if (gen_load_outer == 0)
586 gen_load = TRUE;
587 }
588 else
589 {
590 lvar_T lvar;
591
592 if (lookup_local(*arg, len, &lvar, cctx) == OK)
593 {
594 type = lvar.lv_type;
595 idx = lvar.lv_idx;
596 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100597 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000598 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100599 outer_loop_depth = lvar.lv_loop_depth;
600 outer_loop_idx = lvar.lv_loop_idx;
601 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000602 else
603 gen_load = TRUE;
604 }
605 else
606 {
607 // "var" can be script-local even without using "s:" if it
608 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000609 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000610 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100611 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000612
613 // When evaluating an expression and the name starts with an
614 // uppercase letter it can be a user defined function.
615 // generate_funcref() will fail if the function can't be found.
616 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000617 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000618 }
619 }
620 if (gen_load)
621 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
622 if (gen_load_outer > 0)
623 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100624 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
625 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000626 cctx->ctx_outer_used = TRUE;
627 }
628 }
629
630 *arg = end;
631
632theend:
633 if (res == FAIL && error && called_emsg == prev_called_emsg)
634 semsg(_(e_variable_not_found_str), name);
635 vim_free(name);
636 return res;
637}
638
639/*
640 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100641 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000642 * Returns FAIL if compilation fails.
643 */
644 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100645compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000646{
LemonBoyf3b48952022-05-05 13:53:03 +0100647 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000648 garray_T save_ga = cctx->ctx_instr;
649 int expr_res;
650 int trailing_error;
651 int instr_count;
652 isn_T *instr = NULL;
653
654 // Remove the string type from the stack.
655 --cctx->ctx_type_stack.ga_len;
656
657 // Temporarily reset the list of instructions so that the jump labels are
658 // correct.
659 cctx->ctx_instr.ga_len = 0;
660 cctx->ctx_instr.ga_maxlen = 0;
661 cctx->ctx_instr.ga_data = NULL;
662 expr_res = compile_expr0(&s, cctx);
663 s = skipwhite(s);
664 trailing_error = *s != NUL;
665
666 if (expr_res == FAIL || trailing_error
667 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
668 {
669 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000670 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000671 clear_instr_ga(&cctx->ctx_instr);
672 cctx->ctx_instr = save_ga;
673 ++cctx->ctx_type_stack.ga_len;
674 return FAIL;
675 }
676
677 // Move the generated instructions into the ISN_INSTR instruction, then
678 // restore the list of instructions.
679 instr_count = cctx->ctx_instr.ga_len;
680 instr = cctx->ctx_instr.ga_data;
681 instr[instr_count].isn_type = ISN_FINISH;
682
683 cctx->ctx_instr = save_ga;
684 vim_free(isn->isn_arg.string);
685 isn->isn_type = ISN_INSTR;
686 isn->isn_arg.instr = instr;
687 return OK;
688}
689
690/*
691 * Compile the argument expressions.
692 * "arg" points to just after the "(" and is advanced to after the ")"
693 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100694 int
LemonBoyf3b48952022-05-05 13:53:03 +0100695compile_arguments(
696 char_u **arg,
697 cctx_T *cctx,
698 int *argcount,
699 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000700{
701 char_u *p = *arg;
702 char_u *whitep = *arg;
703 int must_end = FALSE;
704 int instr_count;
705
706 for (;;)
707 {
708 if (may_get_next_line(whitep, &p, cctx) == FAIL)
709 goto failret;
710 if (*p == ')')
711 {
712 *arg = p + 1;
713 return OK;
714 }
715 if (must_end)
716 {
717 semsg(_(e_missing_comma_before_argument_str), p);
718 return FAIL;
719 }
720
721 instr_count = cctx->ctx_instr.ga_len;
722 if (compile_expr0(&p, cctx) == FAIL)
723 return FAIL;
724 ++*argcount;
725
LemonBoyf3b48952022-05-05 13:53:03 +0100726 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000727 && cctx->ctx_instr.ga_len == instr_count + 1)
728 {
729 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
730
731 // {skip} argument of searchpair() can be compiled if not empty
732 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100733 compile_string(isn, cctx, 0);
734 }
735 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
736 && cctx->ctx_instr.ga_len == instr_count + 1)
737 {
738 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
739
740 // {sub} argument of substitute() can be compiled if it starts
741 // with \=
742 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100743 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100744 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000745 }
746
747 if (*p != ',' && *skipwhite(p) == ',')
748 {
749 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
750 p = skipwhite(p);
751 }
752 if (*p == ',')
753 {
754 ++p;
755 if (*p != NUL && !VIM_ISWHITE(*p))
756 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
757 }
758 else
759 must_end = TRUE;
760 whitep = p;
761 p = skipwhite(p);
762 }
763failret:
764 emsg(_(e_missing_closing_paren));
765 return FAIL;
766}
767
768/*
769 * Compile a function call: name(arg1, arg2)
770 * "arg" points to "name", "arg + varlen" to the "(".
771 * "argcount_init" is 1 for "value->method()"
772 * Instructions:
773 * EVAL arg1
774 * EVAL arg2
775 * BCALL / DCALL / UCALL
776 */
777 static int
778compile_call(
779 char_u **arg,
780 size_t varlen,
781 cctx_T *cctx,
782 ppconst_T *ppconst,
783 int argcount_init)
784{
785 char_u *name = *arg;
786 char_u *p;
787 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100788 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000789 char_u fname_buf[FLEN_FIXED + 1];
790 char_u *tofree = NULL;
791 int error = FCERR_NONE;
792 ufunc_T *ufunc = NULL;
793 int res = FAIL;
794 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000795 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100796 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000797 imported_T *import;
798
799 if (varlen >= sizeof(namebuf))
800 {
801 semsg(_(e_name_too_long_str), name);
802 return FAIL;
803 }
804 vim_strncpy(namebuf, *arg, varlen);
805
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000806 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000807 if (import != NULL)
808 {
809 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
810 return FAIL;
811 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000812
813 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100814 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000815 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100816 if ((varlen == 3
817 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000818 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
819 {
820 char_u *s = skipwhite(*arg + varlen + 1);
821 typval_T argvars[2];
822 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100823 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000824
825 argvars[0].v_type = VAR_UNKNOWN;
826 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100827 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000828 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100829 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000830 s = skipwhite(s);
831 if (*s == ')' && argvars[0].v_type == VAR_STRING
832 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
833 || !is_has))
834 {
835 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
836
837 *arg = s + 1;
838 argvars[1].v_type = VAR_UNKNOWN;
839 tv->v_type = VAR_NUMBER;
840 tv->vval.v_number = 0;
841 if (is_has)
842 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100843 else if (is_len)
844 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000845 else
846 f_exists(argvars, tv);
847 clear_tv(&argvars[0]);
848 ++ppconst->pp_used;
849 return OK;
850 }
851 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100852 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853 {
854 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
855 return FAIL;
856 }
857 }
858
859 if (generate_ppconst(cctx, ppconst) == FAIL)
860 return FAIL;
861
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000862 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
863
864 // We handle the "skip" argument of searchpair() and searchpairpos()
865 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100866 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
867 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
868 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
869 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
870 special_fn = CA_SEARCHPAIR;
871 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
872 special_fn = CA_SUBSTITUTE;
873 else
874 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000875
876 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100877 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000878 goto theend;
879
880 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
881 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
882 {
883 int idx;
884
885 // builtin function
886 idx = find_internal_func(name);
887 if (idx >= 0)
888 {
889 if (STRCMP(name, "flatten") == 0)
890 {
891 emsg(_(e_cannot_use_flatten_in_vim9_script));
892 goto theend;
893 }
894
895 if (STRCMP(name, "add") == 0 && argcount == 2)
896 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000897 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000898
899 // add() can be compiled to instructions if we know the type
900 if (type->tt_type == VAR_LIST)
901 {
902 // inline "add(list, item)" so that the type can be checked
903 res = generate_LISTAPPEND(cctx);
904 idx = -1;
905 }
906 else if (type->tt_type == VAR_BLOB)
907 {
908 // inline "add(blob, nr)" so that the type can be checked
909 res = generate_BLOBAPPEND(cctx);
910 idx = -1;
911 }
912 }
913
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100914 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
915 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100916 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100917 // May have the "D" or "R" flag, reserve a variable for a
918 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100919 if (get_defer_var_idx(cctx) == 0)
920 idx = -1;
921 }
922
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000923 if (idx >= 0)
924 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
925 }
926 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100927 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000928 goto theend;
929 }
930
Bram Moolenaar62aec932022-01-29 21:45:34 +0000931 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
932
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000933 // An argument or local variable can be a function reference, this
934 // overrules a function name.
935 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
936 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
937 {
938 // If we can find the function by name generate the right call.
939 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000940 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000941 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000942 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000943 if (!func_is_global(ufunc))
944 {
945 res = generate_CALL(cctx, ufunc, argcount);
946 goto theend;
947 }
948 if (!has_g_namespace
949 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
950 {
951 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100952 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000953 goto theend;
954 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000955 }
956 }
957
958 // If the name is a variable, load it and use PCALL.
959 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000960 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000961 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000962 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000963 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
964 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000965 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000966
967 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
968 goto theend;
969 }
970
971 // If we can find a global function by name generate the right call.
972 if (ufunc != NULL)
973 {
974 res = generate_CALL(cctx, ufunc, argcount);
975 goto theend;
976 }
977
978 // A global function may be defined only later. Need to figure out at
979 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000980 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000981 res = generate_UCALL(cctx, name, argcount);
982 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100983 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000984
985theend:
986 vim_free(tofree);
987 return res;
988}
989
990// like NAMESPACE_CHAR but with 'a' and 'l'.
991#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
992
993/*
994 * Find the end of a variable or function name. Unlike find_name_end() this
995 * does not recognize magic braces.
996 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
997 * Return a pointer to just after the name. Equal to "arg" if there is no
998 * valid name.
999 */
1000 char_u *
1001to_name_end(char_u *arg, int use_namespace)
1002{
1003 char_u *p;
1004
1005 // Quick check for valid starting character.
1006 if (!eval_isnamec1(*arg))
1007 return arg;
1008
1009 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1010 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1011 // and can be used in slice "[n:]".
1012 if (*p == ':' && (p != arg + 1
1013 || !use_namespace
1014 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1015 break;
1016 return p;
1017}
1018
1019/*
1020 * Like to_name_end() but also skip over a list or dict constant.
1021 * Also accept "<SNR>123_Func".
1022 * This intentionally does not handle line continuation.
1023 */
1024 char_u *
1025to_name_const_end(char_u *arg)
1026{
1027 char_u *p = arg;
1028 typval_T rettv;
1029
1030 if (STRNCMP(p, "<SNR>", 5) == 0)
1031 p = skipdigits(p + 5);
1032 p = to_name_end(p, TRUE);
1033 if (p == arg && *arg == '[')
1034 {
1035
1036 // Can be "[1, 2, 3]->Func()".
1037 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1038 p = arg;
1039 }
1040 return p;
1041}
1042
1043/*
1044 * parse a list: [expr, expr]
1045 * "*arg" points to the '['.
1046 * ppconst->pp_is_const is set if all items are a constant.
1047 */
1048 static int
1049compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1050{
1051 char_u *p = skipwhite(*arg + 1);
1052 char_u *whitep = *arg + 1;
1053 int count = 0;
1054 int is_const;
1055 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001056 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001057
1058 for (;;)
1059 {
1060 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1061 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001062 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001063 return FAIL;
1064 }
1065 if (*p == ',')
1066 {
1067 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1068 return FAIL;
1069 }
1070 if (*p == ']')
1071 {
1072 ++p;
1073 break;
1074 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001075 if (must_end)
1076 {
1077 semsg(_(e_missing_comma_in_list_str), p);
1078 return FAIL;
1079 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001080 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1081 return FAIL;
1082 if (!is_const)
1083 is_all_const = FALSE;
1084 ++count;
1085 if (*p == ',')
1086 {
1087 ++p;
1088 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1089 {
1090 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1091 return FAIL;
1092 }
1093 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001094 else
1095 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001096 whitep = p;
1097 p = skipwhite(p);
1098 }
1099 *arg = p;
1100
1101 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001102 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001103}
1104
1105/*
1106 * Parse a lambda: "(arg, arg) => expr"
1107 * "*arg" points to the '('.
1108 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1109 */
1110 static int
1111compile_lambda(char_u **arg, cctx_T *cctx)
1112{
1113 int r;
1114 typval_T rettv;
1115 ufunc_T *ufunc;
1116 evalarg_T evalarg;
1117
1118 init_evalarg(&evalarg);
1119 evalarg.eval_flags = EVAL_EVALUATE;
1120 evalarg.eval_cctx = cctx;
1121
1122 // Get the funcref in "rettv".
1123 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1124 if (r != OK)
1125 {
1126 clear_evalarg(&evalarg, NULL);
1127 return r;
1128 }
1129
1130 // "rettv" will now be a partial referencing the function.
1131 ufunc = rettv.vval.v_partial->pt_func;
1132 ++ufunc->uf_refcount;
1133 clear_tv(&rettv);
1134
1135 // Compile it here to get the return type. The return type is optional,
1136 // when it's missing use t_unknown. This is recognized in
1137 // compile_return().
1138 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1139 ufunc->uf_ret_type = &t_unknown;
1140 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1141
1142 // When the outer function is compiled for profiling or debugging, the
1143 // lambda may be called without profiling or debugging. Compile it here in
1144 // the right context.
1145 if (cctx->ctx_compile_type == CT_DEBUG
1146#ifdef FEAT_PROFILE
1147 || cctx->ctx_compile_type == CT_PROFILE
1148#endif
1149 )
1150 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1151
Bram Moolenaar139575d2022-03-15 19:29:30 +00001152 // if the outer function is not compiled for debugging or profiling, this
1153 // one might be
1154 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001155 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001156 compiletype_T compile_type = get_compile_type(ufunc);
1157
1158 if (compile_type != CT_NONE)
1159 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001160 }
1161
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001162 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1163 // "*arg" may point into it. Point into the original line to avoid a
1164 // dangling pointer.
1165 if (evalarg.eval_using_cmdline)
1166 {
1167 garray_T *gap = &evalarg.eval_tofree_ga;
1168 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1169
1170 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1171 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001172 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001173 }
1174
1175 clear_evalarg(&evalarg, NULL);
1176
1177 if (ufunc->uf_def_status == UF_COMPILED)
1178 {
1179 // The return type will now be known.
1180 set_function_type(ufunc);
1181
1182 // The function reference count will be 1. When the ISN_FUNCREF
1183 // instruction is deleted the reference count is decremented and the
1184 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001185 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001186 }
1187
1188 func_ptr_unref(ufunc);
1189 return FAIL;
1190}
1191
1192/*
1193 * Get a lambda and compile it. Uses Vim9 syntax.
1194 */
1195 int
1196get_lambda_tv_and_compile(
1197 char_u **arg,
1198 typval_T *rettv,
1199 int types_optional,
1200 evalarg_T *evalarg)
1201{
1202 int r;
1203 ufunc_T *ufunc;
1204 int save_sc_version = current_sctx.sc_version;
1205
1206 // Get the funcref in "rettv".
1207 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1208 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1209 current_sctx.sc_version = save_sc_version;
1210 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001211 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001212
1213 // "rettv" will now be a partial referencing the function.
1214 ufunc = rettv->vval.v_partial->pt_func;
1215
1216 // Compile it here to get the return type. The return type is optional,
1217 // when it's missing use t_unknown. This is recognized in
1218 // compile_return().
1219 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1220 ufunc->uf_ret_type = &t_unknown;
1221 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1222
1223 if (ufunc->uf_def_status == UF_COMPILED)
1224 {
1225 // The return type will now be known.
1226 set_function_type(ufunc);
1227 return OK;
1228 }
1229 clear_tv(rettv);
1230 return FAIL;
1231}
1232
1233/*
1234 * parse a dict: {key: val, [key]: val}
1235 * "*arg" points to the '{'.
1236 * ppconst->pp_is_const is set if all item values are a constant.
1237 */
1238 static int
1239compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1240{
1241 garray_T *instr = &cctx->ctx_instr;
1242 int count = 0;
1243 dict_T *d = dict_alloc();
1244 dictitem_T *item;
1245 char_u *whitep = *arg + 1;
1246 char_u *p;
1247 int is_const;
1248 int is_all_const = TRUE; // reset when non-const encountered
1249
1250 if (d == NULL)
1251 return FAIL;
1252 if (generate_ppconst(cctx, ppconst) == FAIL)
1253 return FAIL;
1254 for (;;)
1255 {
1256 char_u *key = NULL;
1257
1258 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1259 {
1260 *arg = NULL;
1261 goto failret;
1262 }
1263
1264 if (**arg == '}')
1265 break;
1266
1267 if (**arg == '[')
1268 {
1269 isn_T *isn;
1270
1271 // {[expr]: value} uses an evaluated key.
1272 *arg = skipwhite(*arg + 1);
1273 if (compile_expr0(arg, cctx) == FAIL)
1274 return FAIL;
1275 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1276 if (isn->isn_type == ISN_PUSHNR)
1277 {
1278 char buf[NUMBUFLEN];
1279
1280 // Convert to string at compile time.
1281 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1282 isn->isn_type = ISN_PUSHS;
1283 isn->isn_arg.string = vim_strsave((char_u *)buf);
1284 }
1285 if (isn->isn_type == ISN_PUSHS)
1286 key = isn->isn_arg.string;
1287 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1288 return FAIL;
1289 *arg = skipwhite(*arg);
1290 if (**arg != ']')
1291 {
1292 emsg(_(e_missing_matching_bracket_after_dict_key));
1293 return FAIL;
1294 }
1295 ++*arg;
1296 }
1297 else
1298 {
1299 // {"name": value},
1300 // {'name': value},
1301 // {name: value} use "name" as a literal key
1302 key = get_literal_key(arg);
1303 if (key == NULL)
1304 return FAIL;
1305 if (generate_PUSHS(cctx, &key) == FAIL)
1306 return FAIL;
1307 }
1308
1309 // Check for duplicate keys, if using string keys.
1310 if (key != NULL)
1311 {
1312 item = dict_find(d, key, -1);
1313 if (item != NULL)
1314 {
dundargocc57b5bc2022-11-02 13:30:51 +00001315 semsg(_(e_duplicate_key_in_dictionary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001316 goto failret;
1317 }
1318 item = dictitem_alloc(key);
1319 if (item != NULL)
1320 {
1321 item->di_tv.v_type = VAR_UNKNOWN;
1322 item->di_tv.v_lock = 0;
1323 if (dict_add(d, item) == FAIL)
1324 dictitem_free(item);
1325 }
1326 }
1327
1328 if (**arg != ':')
1329 {
1330 if (*skipwhite(*arg) == ':')
1331 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1332 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001333 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001334 return FAIL;
1335 }
1336 whitep = *arg + 1;
1337 if (!IS_WHITE_OR_NUL(*whitep))
1338 {
1339 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1340 return FAIL;
1341 }
1342
1343 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1344 {
1345 *arg = NULL;
1346 goto failret;
1347 }
1348
1349 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1350 return FAIL;
1351 if (!is_const)
1352 is_all_const = FALSE;
1353 ++count;
1354
1355 whitep = *arg;
1356 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1357 {
1358 *arg = NULL;
1359 goto failret;
1360 }
1361 if (**arg == '}')
1362 break;
1363 if (**arg != ',')
1364 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001365 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001366 goto failret;
1367 }
1368 if (IS_WHITE_OR_NUL(*whitep))
1369 {
1370 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1371 return FAIL;
1372 }
1373 whitep = *arg + 1;
1374 if (!IS_WHITE_OR_NUL(*whitep))
1375 {
1376 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1377 return FAIL;
1378 }
1379 *arg = skipwhite(whitep);
1380 }
1381
1382 *arg = *arg + 1;
1383
1384 // Allow for following comment, after at least one space.
1385 p = skipwhite(*arg);
1386 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1387 *arg += STRLEN(*arg);
1388
1389 dict_unref(d);
1390 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001391 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001392
1393failret:
1394 if (*arg == NULL)
1395 {
1396 semsg(_(e_missing_dict_end), _("[end of lines]"));
1397 *arg = (char_u *)"";
1398 }
1399 dict_unref(d);
1400 return FAIL;
1401}
1402
1403/*
1404 * Compile "&option".
1405 */
1406 static int
1407compile_get_option(char_u **arg, cctx_T *cctx)
1408{
1409 typval_T rettv;
1410 char_u *start = *arg;
1411 int ret;
1412
1413 // parse the option and get the current value to get the type.
1414 rettv.v_type = VAR_UNKNOWN;
1415 ret = eval_option(arg, &rettv, TRUE);
1416 if (ret == OK)
1417 {
1418 // include the '&' in the name, eval_option() expects it.
1419 char_u *name = vim_strnsave(start, *arg - start);
1420 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1421 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1422
1423 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1424 vim_free(name);
1425 }
1426 clear_tv(&rettv);
1427
1428 return ret;
1429}
1430
1431/*
1432 * Compile "$VAR".
1433 */
1434 static int
1435compile_get_env(char_u **arg, cctx_T *cctx)
1436{
1437 char_u *start = *arg;
1438 int len;
1439 int ret;
1440 char_u *name;
1441
1442 ++*arg;
1443 len = get_env_len(arg);
1444 if (len == 0)
1445 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001446 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001447 return FAIL;
1448 }
1449
1450 // include the '$' in the name, eval_env_var() expects it.
1451 name = vim_strnsave(start, len + 1);
1452 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1453 vim_free(name);
1454 return ret;
1455}
1456
1457/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001458 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001459 */
1460 static int
1461compile_interp_string(char_u **arg, cctx_T *cctx)
1462{
1463 typval_T tv;
1464 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001465 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001466 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001467 int count = 0;
1468 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001469
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001470 // *arg is on the '$' character, move it to the first string character.
1471 ++*arg;
1472 quote = **arg;
1473 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001474
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001475 for (;;)
1476 {
1477 // Get the string up to the matching quote or to a single '{'.
1478 // "arg" is advanced to either the quote or the '{'.
1479 if (quote == '"')
1480 ret = eval_string(arg, &tv, evaluate, TRUE);
1481 else
1482 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1483 if (ret == FAIL)
1484 break;
1485 if (evaluate)
1486 {
1487 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1488 || (**arg != '{' && count == 0))
1489 {
1490 // generate non-empty string or empty string if it's the only
1491 // one
1492 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1493 return FAIL;
1494 tv.vval.v_string = NULL; // don't free it now
1495 ++count;
1496 }
1497 clear_tv(&tv);
1498 }
1499
1500 if (**arg != '{')
1501 {
1502 // found terminating quote
1503 ++*arg;
1504 break;
1505 }
1506
1507 p = compile_one_expr_in_str(*arg, cctx);
1508 if (p == NULL)
1509 {
1510 ret = FAIL;
1511 break;
1512 }
1513 ++count;
1514 *arg = p;
1515 }
LemonBoy2eaef102022-05-06 13:14:50 +01001516
1517 if (ret == FAIL || !evaluate)
1518 return ret;
1519
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001520 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1521 if (count > 1)
1522 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001523
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001524 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001525}
1526
1527/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001528 * Compile "@r".
1529 */
1530 static int
1531compile_get_register(char_u **arg, cctx_T *cctx)
1532{
1533 int ret;
1534
1535 ++*arg;
1536 if (**arg == NUL)
1537 {
1538 semsg(_(e_syntax_error_at_str), *arg - 1);
1539 return FAIL;
1540 }
1541 if (!valid_yank_reg(**arg, FALSE))
1542 {
1543 emsg_invreg(**arg);
1544 return FAIL;
1545 }
1546 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1547 ++*arg;
1548 return ret;
1549}
1550
1551/*
1552 * Apply leading '!', '-' and '+' to constant "rettv".
1553 * When "numeric_only" is TRUE do not apply '!'.
1554 */
1555 static int
1556apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1557{
1558 char_u *p = *end;
1559
1560 // this works from end to start
1561 while (p > start)
1562 {
1563 --p;
1564 if (*p == '-' || *p == '+')
1565 {
1566 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001567 if (rettv->v_type == VAR_FLOAT)
1568 {
1569 if (*p == '-')
1570 rettv->vval.v_float = -rettv->vval.v_float;
1571 }
1572 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001573 {
1574 varnumber_T val;
1575 int error = FALSE;
1576
1577 // tv_get_number_chk() accepts a string, but we don't want that
1578 // here
1579 if (check_not_string(rettv) == FAIL)
1580 return FAIL;
1581 val = tv_get_number_chk(rettv, &error);
1582 clear_tv(rettv);
1583 if (error)
1584 return FAIL;
1585 if (*p == '-')
1586 val = -val;
1587 rettv->v_type = VAR_NUMBER;
1588 rettv->vval.v_number = val;
1589 }
1590 }
1591 else if (numeric_only)
1592 {
1593 ++p;
1594 break;
1595 }
1596 else if (*p == '!')
1597 {
1598 int v = tv2bool(rettv);
1599
1600 // '!' is permissive in the type.
1601 clear_tv(rettv);
1602 rettv->v_type = VAR_BOOL;
1603 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1604 }
1605 }
1606 *end = p;
1607 return OK;
1608}
1609
1610/*
1611 * Recognize v: variables that are constants and set "rettv".
1612 */
1613 static void
1614get_vim_constant(char_u **arg, typval_T *rettv)
1615{
1616 if (STRNCMP(*arg, "v:true", 6) == 0)
1617 {
1618 rettv->v_type = VAR_BOOL;
1619 rettv->vval.v_number = VVAL_TRUE;
1620 *arg += 6;
1621 }
1622 else if (STRNCMP(*arg, "v:false", 7) == 0)
1623 {
1624 rettv->v_type = VAR_BOOL;
1625 rettv->vval.v_number = VVAL_FALSE;
1626 *arg += 7;
1627 }
1628 else if (STRNCMP(*arg, "v:null", 6) == 0)
1629 {
1630 rettv->v_type = VAR_SPECIAL;
1631 rettv->vval.v_number = VVAL_NULL;
1632 *arg += 6;
1633 }
1634 else if (STRNCMP(*arg, "v:none", 6) == 0)
1635 {
1636 rettv->v_type = VAR_SPECIAL;
1637 rettv->vval.v_number = VVAL_NONE;
1638 *arg += 6;
1639 }
1640}
1641
1642 exprtype_T
1643get_compare_type(char_u *p, int *len, int *type_is)
1644{
1645 exprtype_T type = EXPR_UNKNOWN;
1646 int i;
1647
1648 switch (p[0])
1649 {
1650 case '=': if (p[1] == '=')
1651 type = EXPR_EQUAL;
1652 else if (p[1] == '~')
1653 type = EXPR_MATCH;
1654 break;
1655 case '!': if (p[1] == '=')
1656 type = EXPR_NEQUAL;
1657 else if (p[1] == '~')
1658 type = EXPR_NOMATCH;
1659 break;
1660 case '>': if (p[1] != '=')
1661 {
1662 type = EXPR_GREATER;
1663 *len = 1;
1664 }
1665 else
1666 type = EXPR_GEQUAL;
1667 break;
1668 case '<': if (p[1] != '=')
1669 {
1670 type = EXPR_SMALLER;
1671 *len = 1;
1672 }
1673 else
1674 type = EXPR_SEQUAL;
1675 break;
1676 case 'i': if (p[1] == 's')
1677 {
1678 // "is" and "isnot"; but not a prefix of a name
1679 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1680 *len = 5;
1681 i = p[*len];
1682 if (!isalnum(i) && i != '_')
1683 {
1684 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1685 *type_is = TRUE;
1686 }
1687 }
1688 break;
1689 }
1690 return type;
1691}
1692
1693/*
1694 * Skip over an expression, ignoring most errors.
1695 */
1696 void
1697skip_expr_cctx(char_u **arg, cctx_T *cctx)
1698{
1699 evalarg_T evalarg;
1700
1701 init_evalarg(&evalarg);
1702 evalarg.eval_cctx = cctx;
1703 skip_expr(arg, &evalarg);
1704 clear_evalarg(&evalarg, NULL);
1705}
1706
1707/*
1708 * Check that the top of the type stack has a type that can be used as a
1709 * condition. Give an error and return FAIL if not.
1710 */
1711 int
1712bool_on_stack(cctx_T *cctx)
1713{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001714 type_T *type;
1715
Bram Moolenaar078a4612022-01-04 15:17:03 +00001716 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001717 if (type == &t_bool)
1718 return OK;
1719
Bram Moolenaar4913d422022-10-17 13:13:32 +01001720 if (type->tt_type == VAR_ANY
1721 || type->tt_type == VAR_UNKNOWN
1722 || type->tt_type == VAR_NUMBER
1723 || type == &t_number_bool
1724 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001725 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1726 // This requires a runtime type check.
1727 return generate_COND2BOOL(cctx);
1728
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001729 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001730}
1731
1732/*
1733 * Give the "white on both sides" error, taking the operator from "p[len]".
1734 */
1735 void
1736error_white_both(char_u *op, int len)
1737{
1738 char_u buf[10];
1739
1740 vim_strncpy(buf, op, len);
1741 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1742}
1743
1744/*
1745 * Compile code to apply '-', '+' and '!'.
1746 * When "numeric_only" is TRUE do not apply '!'.
1747 */
1748 static int
1749compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1750{
1751 char_u *p = *end;
1752
1753 // this works from end to start
1754 while (p > start)
1755 {
1756 --p;
1757 while (VIM_ISWHITE(*p))
1758 --p;
1759 if (*p == '-' || *p == '+')
1760 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001761 type_T *type = get_type_on_stack(cctx, 0);
1762 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001763 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001764 return FAIL;
1765
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001766 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001767 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1768 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001769 }
1770 else if (numeric_only)
1771 {
1772 ++p;
1773 break;
1774 }
1775 else
1776 {
1777 int invert = *p == '!';
1778
1779 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1780 {
1781 if (p[-1] == '!')
1782 invert = !invert;
1783 --p;
1784 }
1785 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1786 return FAIL;
1787 }
1788 }
1789 *end = p;
1790 return OK;
1791}
1792
1793/*
1794 * Compile "(expression)": recursive!
1795 * Return FAIL/OK.
1796 */
1797 static int
1798compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1799{
1800 int ret;
1801 char_u *p = *arg + 1;
1802
1803 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1804 return FAIL;
1805 if (ppconst->pp_used <= PPSIZE - 10)
1806 {
1807 ret = compile_expr1(arg, cctx, ppconst);
1808 }
1809 else
1810 {
1811 // Not enough space in ppconst, flush constants.
1812 if (generate_ppconst(cctx, ppconst) == FAIL)
1813 return FAIL;
1814 ret = compile_expr0(arg, cctx);
1815 }
1816 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1817 return FAIL;
1818 if (**arg == ')')
1819 ++*arg;
1820 else if (ret == OK)
1821 {
1822 emsg(_(e_missing_closing_paren));
1823 ret = FAIL;
1824 }
1825 return ret;
1826}
1827
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001828static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001829
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001830/*
1831 * Compile whatever comes after "name" or "name()".
1832 * Advances "*arg" only when something was recognized.
1833 */
1834 static int
1835compile_subscript(
1836 char_u **arg,
1837 cctx_T *cctx,
1838 char_u *start_leader,
1839 char_u **end_leader,
1840 ppconst_T *ppconst)
1841{
1842 char_u *name_start = *end_leader;
1843 int keeping_dict = FALSE;
1844
1845 for (;;)
1846 {
1847 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001848 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001849
1850 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1851 {
1852 char_u *next = peek_next_line_from_context(cctx);
1853
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001854 // If a following line starts with "->{", "->(" or "->X" advance to
1855 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001856 // Also if a following line starts with ".x".
1857 if (next != NULL &&
1858 ((next[0] == '-' && next[1] == '>'
1859 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001860 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001861 || ASCII_ISALPHA(*skipwhite(next + 2))))
1862 || (next[0] == '.' && eval_isdictc(next[1]))))
1863 {
1864 next = next_line_from_context(cctx, TRUE);
1865 if (next == NULL)
1866 return FAIL;
1867 *arg = next;
1868 p = skipwhite(*arg);
1869 }
1870 }
1871
1872 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1873 // is not a function call.
1874 if (**arg == '(')
1875 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001876 int argcount = 0;
1877
1878 if (generate_ppconst(cctx, ppconst) == FAIL)
1879 return FAIL;
1880 ppconst->pp_is_const = FALSE;
1881
1882 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001883 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001884
1885 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001886 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001887 return FAIL;
1888 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1889 return FAIL;
1890 if (keeping_dict)
1891 {
1892 keeping_dict = FALSE;
1893 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1894 return FAIL;
1895 }
1896 }
1897 else if (*p == '-' && p[1] == '>')
1898 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001899 char_u *pstart = p;
1900 int alt;
1901 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001902
Bram Moolenaarc7349932022-01-16 20:59:39 +00001903 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001904 if (generate_ppconst(cctx, ppconst) == FAIL)
1905 return FAIL;
1906 ppconst->pp_is_const = FALSE;
1907
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001908 // Apply the '!', '-' and '+' first:
1909 // -1.0->func() works like (-1.0)->func()
1910 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1911 return FAIL;
1912
1913 p += 2;
1914 *arg = skipwhite(p);
1915 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001916
1917 // Three alternatives handled here:
1918 // 1. "base->name(" only a name, use compile_call()
1919 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1920 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001921 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001922 alt = 2;
1923 else
1924 {
1925 // alternative 1 or 3
1926 p = *arg;
1927 if (!eval_isnamec1(*p))
1928 {
1929 semsg(_(e_trailing_characters_str), pstart);
1930 return FAIL;
1931 }
1932 if (ASCII_ISALPHA(*p) && p[1] == ':')
1933 p += 2;
1934 for ( ; eval_isnamec(*p); ++p)
1935 ;
1936 if (*p == '(')
1937 {
1938 // alternative 1
1939 alt = 1;
1940 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1941 return FAIL;
1942 }
1943 else
1944 {
1945 // Must be alternative 3, find the "(". Only works within
1946 // one line.
1947 alt = 3;
1948 paren = vim_strchr(p, '(');
1949 if (paren == NULL)
1950 {
1951 semsg(_(e_missing_parenthesis_str), *arg);
1952 return FAIL;
1953 }
1954 }
1955 }
1956
1957 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001958 {
1959 int argcount = 1;
1960 garray_T *stack = &cctx->ctx_type_stack;
1961 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001962 int expr_isn_start = cctx->ctx_instr.ga_len;
1963 int expr_isn_end;
1964 int arg_isn_count;
1965
Bram Moolenaarc7349932022-01-16 20:59:39 +00001966 if (alt == 2)
1967 {
1968 // Funcref call: list->(Refs[2])(arg)
1969 // or lambda: list->((arg) => expr)(arg)
1970 //
1971 // Fist compile the function expression.
1972 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1973 return FAIL;
1974 }
1975 else
1976 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001977 int fail;
1978 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001979 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001980
Bram Moolenaarc7349932022-01-16 20:59:39 +00001981 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001982
1983 // instead of using LOADG for "import.Func" use PUSHFUNC
1984 ++paren_follows_after_expr;
1985
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001986 // do not look in the next line
1987 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001988
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001989 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001990 || *skipwhite(*arg) != NUL;
1991 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001992 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001993 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001994
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001995 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001996 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001997 if (did_emsg == prev_did_emsg)
1998 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001999 return FAIL;
2000 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002001 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002002
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002003 // Compile the arguments.
2004 if (**arg != '(')
2005 {
2006 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002007 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002008 else
2009 semsg(_(e_missing_parenthesis_str), *arg);
2010 return FAIL;
2011 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002012
2013 // Remember the next instruction index, where the instructions
2014 // for arguments are being written.
2015 expr_isn_end = cctx->ctx_instr.ga_len;
2016
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002017 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002018 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2019 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002020 return FAIL;
2021
2022 // Move the instructions for the arguments to before the
2023 // instructions of the expression and move the type of the
2024 // expression after the argument types. This is what ISN_PCALL
2025 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002026 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2027 if (arg_isn_count > 0)
2028 {
2029 int expr_isn_count = expr_isn_end - expr_isn_start;
2030 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002031 type_T *decl_type;
2032 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002033
2034 if (isn == NULL)
2035 return FAIL;
2036 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2037 + expr_isn_start,
2038 sizeof(isn_T) * expr_isn_count);
2039 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2040 + expr_isn_start,
2041 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2042 sizeof(isn_T) * arg_isn_count);
2043 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2044 + expr_isn_start + arg_isn_count,
2045 isn, sizeof(isn_T) * expr_isn_count);
2046 vim_free(isn);
2047
Bram Moolenaar078a4612022-01-04 15:17:03 +00002048 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2049 type = typep->type_curr;
2050 decl_type = typep->type_decl;
2051 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2052 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2053 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002054 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002055 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2056 typep->type_curr = type;
2057 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002058 }
2059
Bram Moolenaar078a4612022-01-04 15:17:03 +00002060 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002061 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2062 return FAIL;
2063 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002064
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002065 if (keeping_dict)
2066 {
2067 keeping_dict = FALSE;
2068 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2069 return FAIL;
2070 }
2071 }
2072 else if (**arg == '[')
2073 {
2074 int is_slice = FALSE;
2075
2076 // list index: list[123]
2077 // dict member: dict[key]
2078 // string index: text[123]
2079 // blob index: blob[123]
2080 if (generate_ppconst(cctx, ppconst) == FAIL)
2081 return FAIL;
2082 ppconst->pp_is_const = FALSE;
2083
2084 ++p;
2085 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2086 return FAIL;
2087 if (**arg == ':')
2088 {
2089 // missing first index is equal to zero
2090 generate_PUSHNR(cctx, 0);
2091 }
2092 else
2093 {
2094 if (compile_expr0(arg, cctx) == FAIL)
2095 return FAIL;
2096 if (**arg == ':')
2097 {
2098 semsg(_(e_white_space_required_before_and_after_str_at_str),
2099 ":", *arg);
2100 return FAIL;
2101 }
2102 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2103 return FAIL;
2104 *arg = skipwhite(*arg);
2105 }
2106 if (**arg == ':')
2107 {
2108 is_slice = TRUE;
2109 ++*arg;
2110 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2111 {
2112 semsg(_(e_white_space_required_before_and_after_str_at_str),
2113 ":", *arg);
2114 return FAIL;
2115 }
2116 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2117 return FAIL;
2118 if (**arg == ']')
2119 // missing second index is equal to end of string
2120 generate_PUSHNR(cctx, -1);
2121 else
2122 {
2123 if (compile_expr0(arg, cctx) == FAIL)
2124 return FAIL;
2125 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2126 return FAIL;
2127 *arg = skipwhite(*arg);
2128 }
2129 }
2130
2131 if (**arg != ']')
2132 {
2133 emsg(_(e_missing_closing_square_brace));
2134 return FAIL;
2135 }
2136 *arg = *arg + 1;
2137
2138 if (keeping_dict)
2139 {
2140 keeping_dict = FALSE;
2141 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2142 return FAIL;
2143 }
2144 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2145 return FAIL;
2146 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002147 else if (*p == '.'
2148 && (type = get_type_on_stack(cctx, 0)) != &t_unknown
2149 && (type->tt_type == VAR_CLASS || type->tt_type == VAR_OBJECT))
2150 {
2151 // class member: SomeClass.varname
2152 // class method: SomeClass.SomeMethod()
2153 // class constructor: SomeClass.new()
2154 // object member: someObject.varname, this.varname
2155 // object method: someObject.SomeMethod(), this.SomeMethod()
2156 if (compile_class_object_index(cctx, arg, type) == FAIL)
2157 return FAIL;
2158 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002159 else if (*p == '.' && p[1] != '.')
2160 {
2161 // dictionary member: dict.name
2162 if (generate_ppconst(cctx, ppconst) == FAIL)
2163 return FAIL;
2164 ppconst->pp_is_const = FALSE;
2165
2166 *arg = p + 1;
2167 if (IS_WHITE_OR_NUL(**arg))
2168 {
2169 emsg(_(e_missing_name_after_dot));
2170 return FAIL;
2171 }
2172 p = *arg;
2173 if (eval_isdictc(*p))
2174 while (eval_isnamec(*p))
2175 MB_PTR_ADV(p);
2176 if (p == *arg)
2177 {
2178 semsg(_(e_syntax_error_at_str), *arg);
2179 return FAIL;
2180 }
2181 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2182 return FAIL;
2183 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2184 return FAIL;
2185 keeping_dict = TRUE;
2186 *arg = p;
2187 }
2188 else
2189 break;
2190 }
2191
2192 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2193 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002194 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2195 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002196 return FAIL;
2197
2198 return OK;
2199}
2200
2201/*
2202 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2203 * "arg" is advanced until after the expression, skipping white space.
2204 *
2205 * If the value is a constant "ppconst->pp_used" will be non-zero.
2206 * Before instructions are generated, any values in "ppconst" will generated.
2207 *
2208 * This is the compiling equivalent of eval1(), eval2(), etc.
2209 */
2210
2211/*
2212 * number number constant
2213 * 0zFFFFFFFF Blob constant
2214 * "string" string constant
2215 * 'string' literal string constant
2216 * &option-name option value
2217 * @r register contents
2218 * identifier variable value
2219 * function() function call
2220 * $VAR environment variable
2221 * (expression) nested expression
2222 * [expr, expr] List
2223 * {key: val, [key]: val} Dictionary
2224 *
2225 * Also handle:
2226 * ! in front logical NOT
2227 * - in front unary minus
2228 * + in front unary plus (ignored)
2229 * trailing (arg) funcref/partial call
2230 * trailing [] subscript in String or List
2231 * trailing .name entry in Dictionary
2232 * trailing ->name() method call
2233 */
2234 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002235compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002236 char_u **arg,
2237 cctx_T *cctx,
2238 ppconst_T *ppconst)
2239{
2240 char_u *start_leader, *end_leader;
2241 int ret = OK;
2242 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2243 int used_before = ppconst->pp_used;
2244
2245 ppconst->pp_is_const = FALSE;
2246
2247 /*
2248 * Skip '!', '-' and '+' characters. They are handled later.
2249 */
2250 start_leader = *arg;
2251 if (eval_leader(arg, TRUE) == FAIL)
2252 return FAIL;
2253 end_leader = *arg;
2254
2255 rettv->v_type = VAR_UNKNOWN;
2256 switch (**arg)
2257 {
2258 /*
2259 * Number constant.
2260 */
2261 case '0': // also for blob starting with 0z
2262 case '1':
2263 case '2':
2264 case '3':
2265 case '4':
2266 case '5':
2267 case '6':
2268 case '7':
2269 case '8':
2270 case '9':
2271 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2272 return FAIL;
2273 // Apply "-" and "+" just before the number now, right to
2274 // left. Matters especially when "->" follows. Stops at
2275 // '!'.
2276 if (apply_leader(rettv, TRUE,
2277 start_leader, &end_leader) == FAIL)
2278 {
2279 clear_tv(rettv);
2280 return FAIL;
2281 }
2282 break;
2283
2284 /*
2285 * String constant: "string".
2286 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002287 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002288 return FAIL;
2289 break;
2290
2291 /*
2292 * Literal string constant: 'str''ing'.
2293 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002294 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002295 return FAIL;
2296 break;
2297
2298 /*
2299 * Constant Vim variable.
2300 */
2301 case 'v': get_vim_constant(arg, rettv);
2302 ret = NOTDONE;
2303 break;
2304
2305 /*
2306 * "true" constant
2307 */
2308 case 't': if (STRNCMP(*arg, "true", 4) == 0
2309 && !eval_isnamec((*arg)[4]))
2310 {
2311 *arg += 4;
2312 rettv->v_type = VAR_BOOL;
2313 rettv->vval.v_number = VVAL_TRUE;
2314 }
2315 else
2316 ret = NOTDONE;
2317 break;
2318
2319 /*
2320 * "false" constant
2321 */
2322 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2323 && !eval_isnamec((*arg)[5]))
2324 {
2325 *arg += 5;
2326 rettv->v_type = VAR_BOOL;
2327 rettv->vval.v_number = VVAL_FALSE;
2328 }
2329 else
2330 ret = NOTDONE;
2331 break;
2332
2333 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002334 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002335 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002336 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002337 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002338 char_u *p = *arg + 4;
2339 int len;
2340
2341 for (len = 0; eval_isnamec(p[len]); ++len)
2342 ;
2343 ret = handle_predefined(*arg, len + 4, rettv);
2344 if (ret == FAIL)
2345 ret = NOTDONE;
2346 else
2347 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002348 }
2349 else
2350 ret = NOTDONE;
2351 break;
2352
2353 /*
2354 * List: [expr, expr]
2355 */
2356 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2357 return FAIL;
2358 ret = compile_list(arg, cctx, ppconst);
2359 break;
2360
2361 /*
2362 * Dictionary: {'key': val, 'key': val}
2363 */
2364 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2365 return FAIL;
2366 ret = compile_dict(arg, cctx, ppconst);
2367 break;
2368
2369 /*
2370 * Option value: &name
2371 */
2372 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2373 return FAIL;
2374 ret = compile_get_option(arg, cctx);
2375 break;
2376
2377 /*
2378 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002379 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002380 */
2381 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2382 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002383 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2384 ret = compile_interp_string(arg, cctx);
2385 else
2386 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002387 break;
2388
2389 /*
2390 * Register contents: @r.
2391 */
2392 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2393 return FAIL;
2394 ret = compile_get_register(arg, cctx);
2395 break;
2396 /*
2397 * nested expression: (expression).
2398 * lambda: (arg, arg) => expr
2399 * funcref: (arg, arg) => { statement }
2400 */
2401 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2402 ret = compile_lambda(arg, cctx);
2403 if (ret == NOTDONE)
2404 ret = compile_parenthesis(arg, cctx, ppconst);
2405 break;
2406
2407 default: ret = NOTDONE;
2408 break;
2409 }
2410 if (ret == FAIL)
2411 return FAIL;
2412
2413 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2414 {
2415 if (cctx->ctx_skip == SKIP_YES)
2416 clear_tv(rettv);
2417 else
2418 // A constant expression can possibly be handled compile time,
2419 // return the value instead of generating code.
2420 ++ppconst->pp_used;
2421 }
2422 else if (ret == NOTDONE)
2423 {
2424 char_u *p;
2425 int r;
2426
2427 if (!eval_isnamec1(**arg))
2428 {
2429 if (!vim9_bad_comment(*arg))
2430 {
2431 if (ends_excmd(*skipwhite(*arg)))
2432 semsg(_(e_empty_expression_str), *arg);
2433 else
2434 semsg(_(e_name_expected_str), *arg);
2435 }
2436 return FAIL;
2437 }
2438
2439 // "name" or "name()"
2440 p = to_name_end(*arg, TRUE);
2441 if (p - *arg == (size_t)1 && **arg == '_')
2442 {
2443 emsg(_(e_cannot_use_underscore_here));
2444 return FAIL;
2445 }
2446
2447 if (*p == '(')
2448 {
2449 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2450 }
2451 else
2452 {
2453 if (cctx->ctx_skip != SKIP_YES
2454 && generate_ppconst(cctx, ppconst) == FAIL)
2455 return FAIL;
2456 r = compile_load(arg, p, cctx, TRUE, TRUE);
2457 }
2458 if (r == FAIL)
2459 return FAIL;
2460 }
2461
2462 // Handle following "[]", ".member", etc.
2463 // Then deal with prefixed '-', '+' and '!', if not done already.
2464 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2465 ppconst) == FAIL)
2466 return FAIL;
2467 if (ppconst->pp_used > 0)
2468 {
2469 // apply the '!', '-' and '+' before the constant
2470 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2471 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2472 return FAIL;
2473 return OK;
2474 }
2475 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2476 return FAIL;
2477 return OK;
2478}
2479
2480/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002481 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002482 */
2483 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002484compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002485{
2486 type_T *want_type = NULL;
2487
2488 // Recognize <type>
2489 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2490 {
2491 ++*arg;
2492 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2493 if (want_type == NULL)
2494 return FAIL;
2495
2496 if (**arg != '>')
2497 {
2498 if (*skipwhite(*arg) == '>')
2499 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2500 else
2501 emsg(_(e_missing_gt));
2502 return FAIL;
2503 }
2504 ++*arg;
2505 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2506 return FAIL;
2507 }
2508
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002509 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002510 return FAIL;
2511
2512 if (want_type != NULL)
2513 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002514 type_T *actual;
2515 where_T where = WHERE_INIT;
2516
2517 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002518 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002519 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002520 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002521 if (need_type(actual, want_type, FALSE,
2522 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002523 return FAIL;
2524 }
2525 }
2526
2527 return OK;
2528}
2529
2530/*
2531 * * number multiplication
2532 * / number division
2533 * % number modulo
2534 */
2535 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002536compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002537{
2538 char_u *op;
2539 char_u *next;
2540 int ppconst_used = ppconst->pp_used;
2541
2542 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002543 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002544 return FAIL;
2545
2546 /*
2547 * Repeat computing, until no "*", "/" or "%" is following.
2548 */
2549 for (;;)
2550 {
2551 op = may_peek_next_line(cctx, *arg, &next);
2552 if (*op != '*' && *op != '/' && *op != '%')
2553 break;
2554 if (next != NULL)
2555 {
2556 *arg = next_line_from_context(cctx, TRUE);
2557 op = skipwhite(*arg);
2558 }
2559
2560 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2561 {
2562 error_white_both(op, 1);
2563 return FAIL;
2564 }
2565 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2566 return FAIL;
2567
2568 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002569 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002570 return FAIL;
2571
2572 if (ppconst->pp_used == ppconst_used + 2
2573 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2574 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2575 {
2576 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2577 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2578 varnumber_T res = 0;
2579 int failed = FALSE;
2580
2581 // both are numbers: compute the result
2582 switch (*op)
2583 {
2584 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2585 break;
2586 case '/': res = num_divide(tv1->vval.v_number,
2587 tv2->vval.v_number, &failed);
2588 break;
2589 case '%': res = num_modulus(tv1->vval.v_number,
2590 tv2->vval.v_number, &failed);
2591 break;
2592 }
2593 if (failed)
2594 return FAIL;
2595 tv1->vval.v_number = res;
2596 --ppconst->pp_used;
2597 }
2598 else
2599 {
2600 generate_ppconst(cctx, ppconst);
2601 generate_two_op(cctx, op);
2602 }
2603 }
2604
2605 return OK;
2606}
2607
2608/*
2609 * + number addition or list/blobl concatenation
2610 * - number subtraction
2611 * .. string concatenation
2612 */
2613 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002614compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002615{
2616 char_u *op;
2617 char_u *next;
2618 int oplen;
2619 int ppconst_used = ppconst->pp_used;
2620
2621 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002622 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002623 return FAIL;
2624
2625 /*
2626 * Repeat computing, until no "+", "-" or ".." is following.
2627 */
2628 for (;;)
2629 {
2630 op = may_peek_next_line(cctx, *arg, &next);
2631 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2632 break;
2633 if (op[0] == op[1] && *op != '.' && next)
2634 // Finding "++" or "--" on the next line is a separate command.
2635 // But ".." is concatenation.
2636 break;
2637 oplen = (*op == '.' ? 2 : 1);
2638 if (next != NULL)
2639 {
2640 *arg = next_line_from_context(cctx, TRUE);
2641 op = skipwhite(*arg);
2642 }
2643
2644 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2645 {
2646 error_white_both(op, oplen);
2647 return FAIL;
2648 }
2649
2650 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2651 return FAIL;
2652
2653 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002654 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002655 return FAIL;
2656
2657 if (ppconst->pp_used == ppconst_used + 2
2658 && (*op == '.'
2659 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2660 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2661 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2662 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2663 {
2664 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2665 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2666
2667 // concat/subtract/add constant numbers
2668 if (*op == '+')
2669 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2670 else if (*op == '-')
2671 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2672 else
2673 {
2674 // concatenate constant strings
2675 char_u *s1 = tv1->vval.v_string;
2676 char_u *s2 = tv2->vval.v_string;
2677 size_t len1 = STRLEN(s1);
2678
2679 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2680 if (tv1->vval.v_string == NULL)
2681 {
2682 clear_ppconst(ppconst);
2683 return FAIL;
2684 }
2685 mch_memmove(tv1->vval.v_string, s1, len1);
2686 STRCPY(tv1->vval.v_string + len1, s2);
2687 vim_free(s1);
2688 vim_free(s2);
2689 }
2690 --ppconst->pp_used;
2691 }
2692 else
2693 {
2694 generate_ppconst(cctx, ppconst);
2695 ppconst->pp_is_const = FALSE;
2696 if (*op == '.')
2697 {
2698 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2699 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2700 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002701 if (generate_CONCAT(cctx, 2) == FAIL)
2702 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002703 }
2704 else
2705 generate_two_op(cctx, op);
2706 }
2707 }
2708
2709 return OK;
2710}
2711
2712/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002713 * expr6a >> expr6b
2714 * expr6a << expr6b
2715 *
2716 * Produces instructions:
2717 * OPNR bitwise left or right shift
2718 */
2719 static int
2720compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2721{
2722 exprtype_T type = EXPR_UNKNOWN;
2723 char_u *p;
2724 char_u *next;
2725 int len = 2;
2726 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002727 isn_T *isn;
2728
2729 // get the first variable
2730 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2731 return FAIL;
2732
2733 /*
2734 * Repeat computing, until no "+", "-" or ".." is following.
2735 */
2736 for (;;)
2737 {
2738 type = EXPR_UNKNOWN;
2739
2740 p = may_peek_next_line(cctx, *arg, &next);
2741 if (p[0] == '<' && p[1] == '<')
2742 type = EXPR_LSHIFT;
2743 else if (p[0] == '>' && p[1] == '>')
2744 type = EXPR_RSHIFT;
2745
2746 if (type == EXPR_UNKNOWN)
2747 return OK;
2748
2749 // Handle a bitwise left or right shift operator
2750 if (ppconst->pp_used == ppconst_used + 1)
2751 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002752 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002753 {
2754 // left operand should be a number
2755 emsg(_(e_bitshift_ops_must_be_number));
2756 return FAIL;
2757 }
2758 }
2759 else
2760 {
2761 type_T *t = get_type_on_stack(cctx, 0);
2762
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002763 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002764 {
2765 emsg(_(e_bitshift_ops_must_be_number));
2766 return FAIL;
2767 }
2768 }
2769
2770 if (next != NULL)
2771 {
2772 *arg = next_line_from_context(cctx, TRUE);
2773 p = skipwhite(*arg);
2774 }
2775
2776 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2777 {
2778 error_white_both(p, len);
2779 return FAIL;
2780 }
2781
2782 // get the second variable
2783 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2784 return FAIL;
2785
2786 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2787 return FAIL;
2788
2789 if (ppconst->pp_used == ppconst_used + 2)
2790 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002791 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2792 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2793
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002794 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002795 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2796 {
2797 // right operand should be a positive number
2798 if (tv2->v_type != VAR_NUMBER)
2799 emsg(_(e_bitshift_ops_must_be_number));
2800 else
dundargocc57b5bc2022-11-02 13:30:51 +00002801 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002802 return FAIL;
2803 }
2804
2805 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2806 tv1->vval.v_number = 0;
2807 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002808 tv1->vval.v_number =
2809 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002810 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002811 tv1->vval.v_number =
2812 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002813 clear_tv(tv2);
2814 --ppconst->pp_used;
2815 }
2816 else
2817 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002818 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2819 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002820 {
2821 emsg(_(e_bitshift_ops_must_be_number));
2822 return FAIL;
2823 }
2824
2825 generate_ppconst(cctx, ppconst);
2826
2827 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2828 if (isn == NULL)
2829 return FAIL;
2830
2831 if (isn != NULL)
2832 isn->isn_arg.op.op_type = type;
2833 }
2834 }
2835
2836 return OK;
2837}
2838
2839/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002840 * expr5a == expr5b
2841 * expr5a =~ expr5b
2842 * expr5a != expr5b
2843 * expr5a !~ expr5b
2844 * expr5a > expr5b
2845 * expr5a >= expr5b
2846 * expr5a < expr5b
2847 * expr5a <= expr5b
2848 * expr5a is expr5b
2849 * expr5a isnot expr5b
2850 *
2851 * Produces instructions:
2852 * EVAL expr5a Push result of "expr5a"
2853 * EVAL expr5b Push result of "expr5b"
2854 * COMPARE one of the compare instructions
2855 */
2856 static int
2857compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2858{
2859 exprtype_T type = EXPR_UNKNOWN;
2860 char_u *p;
2861 char_u *next;
2862 int len = 2;
2863 int type_is = FALSE;
2864 int ppconst_used = ppconst->pp_used;
2865
2866 // get the first variable
2867 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2868 return FAIL;
2869
2870 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002871
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002872 type = get_compare_type(p, &len, &type_is);
2873
2874 /*
2875 * If there is a comparative operator, use it.
2876 */
2877 if (type != EXPR_UNKNOWN)
2878 {
2879 int ic = FALSE; // Default: do not ignore case
2880
2881 if (next != NULL)
2882 {
2883 *arg = next_line_from_context(cctx, TRUE);
2884 p = skipwhite(*arg);
2885 }
2886 if (type_is && (p[len] == '?' || p[len] == '#'))
2887 {
2888 semsg(_(e_invalid_expression_str), *arg);
2889 return FAIL;
2890 }
2891 // extra question mark appended: ignore case
2892 if (p[len] == '?')
2893 {
2894 ic = TRUE;
2895 ++len;
2896 }
2897 // extra '#' appended: match case (ignored)
2898 else if (p[len] == '#')
2899 ++len;
2900 // nothing appended: match case
2901
2902 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2903 {
2904 error_white_both(p, len);
2905 return FAIL;
2906 }
2907
2908 // get the second variable
2909 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2910 return FAIL;
2911
2912 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2913 return FAIL;
2914
2915 if (ppconst->pp_used == ppconst_used + 2)
2916 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002917 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002918 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2919 int ret;
2920
2921 // Both sides are a constant, compute the result now.
2922 // First check for a valid combination of types, this is more
2923 // strict than typval_compare().
2924 if (check_compare_types(type, tv1, tv2) == FAIL)
2925 ret = FAIL;
2926 else
2927 {
2928 ret = typval_compare(tv1, tv2, type, ic);
2929 tv1->v_type = VAR_BOOL;
2930 tv1->vval.v_number = tv1->vval.v_number
2931 ? VVAL_TRUE : VVAL_FALSE;
2932 clear_tv(tv2);
2933 --ppconst->pp_used;
2934 }
2935 return ret;
2936 }
2937
2938 generate_ppconst(cctx, ppconst);
2939 return generate_COMPARE(cctx, type, ic);
2940 }
2941
2942 return OK;
2943}
2944
2945static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2946
2947/*
2948 * Compile || or &&.
2949 */
2950 static int
2951compile_and_or(
2952 char_u **arg,
2953 cctx_T *cctx,
2954 char *op,
2955 ppconst_T *ppconst,
2956 int ppconst_used UNUSED)
2957{
2958 char_u *next;
2959 char_u *p = may_peek_next_line(cctx, *arg, &next);
2960 int opchar = *op;
2961
2962 if (p[0] == opchar && p[1] == opchar)
2963 {
2964 garray_T *instr = &cctx->ctx_instr;
2965 garray_T end_ga;
2966 int save_skip = cctx->ctx_skip;
2967
2968 /*
2969 * Repeat until there is no following "||" or "&&"
2970 */
2971 ga_init2(&end_ga, sizeof(int), 10);
2972 while (p[0] == opchar && p[1] == opchar)
2973 {
2974 long start_lnum = SOURCING_LNUM;
2975 long save_sourcing_lnum;
2976 int start_ctx_lnum = cctx->ctx_lnum;
2977 int save_lnum;
2978 int const_used;
2979 int status;
2980 jumpwhen_T jump_when = opchar == '|'
2981 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2982
2983 if (next != NULL)
2984 {
2985 *arg = next_line_from_context(cctx, TRUE);
2986 p = skipwhite(*arg);
2987 }
2988
2989 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2990 {
2991 semsg(_(e_white_space_required_before_and_after_str_at_str),
2992 op, p);
2993 ga_clear(&end_ga);
2994 return FAIL;
2995 }
2996
2997 save_sourcing_lnum = SOURCING_LNUM;
2998 SOURCING_LNUM = start_lnum;
2999 save_lnum = cctx->ctx_lnum;
3000 cctx->ctx_lnum = start_ctx_lnum;
3001
3002 status = check_ppconst_bool(ppconst);
3003 if (status != FAIL)
3004 {
3005 // Use the last ppconst if possible.
3006 if (ppconst->pp_used > 0)
3007 {
3008 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3009 int is_true = tv2bool(tv);
3010
3011 if ((is_true && opchar == '|')
3012 || (!is_true && opchar == '&'))
3013 {
3014 // For "false && expr" and "true || expr" the "expr"
3015 // does not need to be evaluated.
3016 cctx->ctx_skip = SKIP_YES;
3017 clear_tv(tv);
3018 tv->v_type = VAR_BOOL;
3019 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3020 }
3021 else
3022 {
3023 // For "true && expr" and "false || expr" only "expr"
3024 // needs to be evaluated.
3025 --ppconst->pp_used;
3026 jump_when = JUMP_NEVER;
3027 }
3028 }
3029 else
3030 {
3031 // Every part must evaluate to a bool.
3032 status = bool_on_stack(cctx);
3033 }
3034 }
3035 if (status != FAIL)
3036 status = ga_grow(&end_ga, 1);
3037 cctx->ctx_lnum = save_lnum;
3038 if (status == FAIL)
3039 {
3040 ga_clear(&end_ga);
3041 return FAIL;
3042 }
3043
3044 if (jump_when != JUMP_NEVER)
3045 {
3046 if (cctx->ctx_skip != SKIP_YES)
3047 {
3048 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3049 ++end_ga.ga_len;
3050 }
3051 generate_JUMP(cctx, jump_when, 0);
3052 }
3053
3054 // eval the next expression
3055 SOURCING_LNUM = save_sourcing_lnum;
3056 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3057 {
3058 ga_clear(&end_ga);
3059 return FAIL;
3060 }
3061
3062 const_used = ppconst->pp_used;
3063 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3064 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3065 {
3066 ga_clear(&end_ga);
3067 return FAIL;
3068 }
3069
3070 // "0 || 1" results in true, "1 && 0" results in false.
3071 if (ppconst->pp_used == const_used + 1)
3072 {
3073 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3074
3075 if (tv->v_type == VAR_NUMBER
3076 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3077 {
3078 tv->vval.v_number = tv->vval.v_number == 1
3079 ? VVAL_TRUE : VVAL_FALSE;
3080 tv->v_type = VAR_BOOL;
3081 }
3082 }
3083
3084 p = may_peek_next_line(cctx, *arg, &next);
3085 }
3086
3087 if (check_ppconst_bool(ppconst) == FAIL)
3088 {
3089 ga_clear(&end_ga);
3090 return FAIL;
3091 }
3092
3093 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3094 // Every part must evaluate to a bool.
3095 if (bool_on_stack(cctx) == FAIL)
3096 {
3097 ga_clear(&end_ga);
3098 return FAIL;
3099 }
3100
3101 if (end_ga.ga_len > 0)
3102 {
3103 // Fill in the end label in all jumps.
3104 generate_ppconst(cctx, ppconst);
3105 while (end_ga.ga_len > 0)
3106 {
3107 isn_T *isn;
3108
3109 --end_ga.ga_len;
3110 isn = ((isn_T *)instr->ga_data)
3111 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3112 isn->isn_arg.jump.jump_where = instr->ga_len;
3113 }
3114 }
3115 ga_clear(&end_ga);
3116
3117 cctx->ctx_skip = save_skip;
3118 }
3119
3120 return OK;
3121}
3122
3123/*
3124 * expr4a && expr4a && expr4a logical AND
3125 *
3126 * Produces instructions:
3127 * EVAL expr4a Push result of "expr4a"
3128 * COND2BOOL convert to bool if needed
3129 * JUMP_IF_COND_FALSE end
3130 * EVAL expr4b Push result of "expr4b"
3131 * JUMP_IF_COND_FALSE end
3132 * EVAL expr4c Push result of "expr4c"
3133 * end:
3134 */
3135 static int
3136compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3137{
3138 int ppconst_used = ppconst->pp_used;
3139
3140 // get the first variable
3141 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3142 return FAIL;
3143
3144 // || and && work almost the same
3145 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3146}
3147
3148/*
3149 * expr3a || expr3b || expr3c logical OR
3150 *
3151 * Produces instructions:
3152 * EVAL expr3a Push result of "expr3a"
3153 * COND2BOOL convert to bool if needed
3154 * JUMP_IF_COND_TRUE end
3155 * EVAL expr3b Push result of "expr3b"
3156 * JUMP_IF_COND_TRUE end
3157 * EVAL expr3c Push result of "expr3c"
3158 * end:
3159 */
3160 static int
3161compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3162{
3163 int ppconst_used = ppconst->pp_used;
3164
3165 // eval the first expression
3166 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3167 return FAIL;
3168
3169 // || and && work almost the same
3170 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3171}
3172
3173/*
3174 * Toplevel expression: expr2 ? expr1a : expr1b
3175 * Produces instructions:
3176 * EVAL expr2 Push result of "expr2"
3177 * JUMP_IF_FALSE alt jump if false
3178 * EVAL expr1a
3179 * JUMP_ALWAYS end
3180 * alt: EVAL expr1b
3181 * end:
3182 *
3183 * Toplevel expression: expr2 ?? expr1
3184 * Produces instructions:
3185 * EVAL expr2 Push result of "expr2"
3186 * JUMP_AND_KEEP_IF_TRUE end jump if true
3187 * EVAL expr1
3188 * end:
3189 */
3190 int
3191compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3192{
3193 char_u *p;
3194 int ppconst_used = ppconst->pp_used;
3195 char_u *next;
3196
3197 // Ignore all kinds of errors when not producing code.
3198 if (cctx->ctx_skip == SKIP_YES)
3199 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003200 int prev_did_emsg = did_emsg;
3201
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003202 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003203 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003204 }
3205
3206 // Evaluate the first expression.
3207 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3208 return FAIL;
3209
3210 p = may_peek_next_line(cctx, *arg, &next);
3211 if (*p == '?')
3212 {
3213 int op_falsy = p[1] == '?';
3214 garray_T *instr = &cctx->ctx_instr;
3215 garray_T *stack = &cctx->ctx_type_stack;
3216 int alt_idx = instr->ga_len;
3217 int end_idx = 0;
3218 isn_T *isn;
3219 type_T *type1 = NULL;
3220 int has_const_expr = FALSE;
3221 int const_value = FALSE;
3222 int save_skip = cctx->ctx_skip;
3223
3224 if (next != NULL)
3225 {
3226 *arg = next_line_from_context(cctx, TRUE);
3227 p = skipwhite(*arg);
3228 }
3229
3230 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3231 {
3232 semsg(_(e_white_space_required_before_and_after_str_at_str),
3233 op_falsy ? "??" : "?", p);
3234 return FAIL;
3235 }
3236
3237 if (ppconst->pp_used == ppconst_used + 1)
3238 {
3239 // the condition is a constant, we know whether the ? or the :
3240 // expression is to be evaluated.
3241 has_const_expr = TRUE;
3242 if (op_falsy)
3243 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3244 else
3245 {
3246 int error = FALSE;
3247
3248 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3249 &error);
3250 if (error)
3251 return FAIL;
3252 }
3253 cctx->ctx_skip = save_skip == SKIP_YES ||
3254 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3255
3256 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3257 // "left ?? right" and "left" is truthy: produce "left"
3258 generate_ppconst(cctx, ppconst);
3259 else
3260 {
3261 clear_tv(&ppconst->pp_tv[ppconst_used]);
3262 --ppconst->pp_used;
3263 }
3264 }
3265 else
3266 {
3267 generate_ppconst(cctx, ppconst);
3268 if (op_falsy)
3269 end_idx = instr->ga_len;
3270 generate_JUMP(cctx, op_falsy
3271 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3272 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003273 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003274 }
3275
3276 // evaluate the second expression; any type is accepted
3277 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3278 return FAIL;
3279 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3280 return FAIL;
3281
3282 if (!has_const_expr)
3283 {
3284 generate_ppconst(cctx, ppconst);
3285
3286 if (!op_falsy)
3287 {
3288 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003289 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003290 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003291
3292 end_idx = instr->ga_len;
3293 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3294
3295 // jump here from JUMP_IF_FALSE
3296 isn = ((isn_T *)instr->ga_data) + alt_idx;
3297 isn->isn_arg.jump.jump_where = instr->ga_len;
3298 }
3299 }
3300
3301 if (!op_falsy)
3302 {
3303 // Check for the ":".
3304 p = may_peek_next_line(cctx, *arg, &next);
3305 if (*p != ':')
3306 {
3307 emsg(_(e_missing_colon_after_questionmark));
3308 return FAIL;
3309 }
3310 if (next != NULL)
3311 {
3312 *arg = next_line_from_context(cctx, TRUE);
3313 p = skipwhite(*arg);
3314 }
3315
3316 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3317 {
3318 semsg(_(e_white_space_required_before_and_after_str_at_str),
3319 ":", p);
3320 return FAIL;
3321 }
3322
3323 // evaluate the third expression
3324 if (has_const_expr)
3325 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3326 ? SKIP_YES : SKIP_NOT;
3327 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3328 return FAIL;
3329 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3330 return FAIL;
3331 }
3332
3333 if (!has_const_expr)
3334 {
3335 type_T **typep;
3336
3337 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003338 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003339
3340 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003341 typep = &((((type2_T *)stack->ga_data)
3342 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003343 common_type(type1, *typep, typep, cctx->ctx_type_list);
3344
3345 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3346 isn = ((isn_T *)instr->ga_data) + end_idx;
3347 isn->isn_arg.jump.jump_where = instr->ga_len;
3348 }
3349
3350 cctx->ctx_skip = save_skip;
3351 }
3352 return OK;
3353}
3354
3355/*
3356 * Toplevel expression.
3357 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3358 * Returns OK or FAIL.
3359 */
3360 int
3361compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3362{
3363 ppconst_T ppconst;
3364
3365 CLEAR_FIELD(ppconst);
3366 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3367 {
3368 clear_ppconst(&ppconst);
3369 return FAIL;
3370 }
3371 if (is_const != NULL)
3372 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3373 if (generate_ppconst(cctx, &ppconst) == FAIL)
3374 return FAIL;
3375 return OK;
3376}
3377
3378/*
3379 * Toplevel expression.
3380 */
3381 int
3382compile_expr0(char_u **arg, cctx_T *cctx)
3383{
3384 return compile_expr0_ext(arg, cctx, NULL);
3385}
3386
3387
3388#endif // defined(FEAT_EVAL)