blob: 4a3527f05755cac0084c59971024640aa753c2e7 [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 {
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000590 lvar_T lvar;
591 class_T *cl = NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000592
593 if (lookup_local(*arg, len, &lvar, cctx) == OK)
594 {
595 type = lvar.lv_type;
596 idx = lvar.lv_idx;
597 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100598 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000599 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100600 outer_loop_depth = lvar.lv_loop_depth;
601 outer_loop_idx = lvar.lv_loop_idx;
602 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000603 else
604 gen_load = TRUE;
605 }
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000606 else if ((idx = class_member_index(*arg, len, &cl, cctx)) >= 0)
Bram Moolenaar6bafdd42023-01-01 12:58:33 +0000607 {
608 res = generate_CLASSMEMBER(cctx, TRUE, cl, idx);
609 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000610 else
611 {
612 // "var" can be script-local even without using "s:" if it
613 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000614 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000615 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100616 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000617
618 // When evaluating an expression and the name starts with an
619 // uppercase letter it can be a user defined function.
620 // generate_funcref() will fail if the function can't be found.
621 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000622 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000623 }
624 }
625 if (gen_load)
626 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
627 if (gen_load_outer > 0)
628 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100629 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
630 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000631 cctx->ctx_outer_used = TRUE;
632 }
633 }
634
635 *arg = end;
636
637theend:
638 if (res == FAIL && error && called_emsg == prev_called_emsg)
639 semsg(_(e_variable_not_found_str), name);
640 vim_free(name);
641 return res;
642}
643
644/*
645 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100646 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000647 * Returns FAIL if compilation fails.
648 */
649 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100650compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000651{
LemonBoyf3b48952022-05-05 13:53:03 +0100652 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000653 garray_T save_ga = cctx->ctx_instr;
654 int expr_res;
655 int trailing_error;
656 int instr_count;
657 isn_T *instr = NULL;
658
659 // Remove the string type from the stack.
660 --cctx->ctx_type_stack.ga_len;
661
662 // Temporarily reset the list of instructions so that the jump labels are
663 // correct.
664 cctx->ctx_instr.ga_len = 0;
665 cctx->ctx_instr.ga_maxlen = 0;
666 cctx->ctx_instr.ga_data = NULL;
667 expr_res = compile_expr0(&s, cctx);
668 s = skipwhite(s);
669 trailing_error = *s != NUL;
670
671 if (expr_res == FAIL || trailing_error
672 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
673 {
674 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000675 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000676 clear_instr_ga(&cctx->ctx_instr);
677 cctx->ctx_instr = save_ga;
678 ++cctx->ctx_type_stack.ga_len;
679 return FAIL;
680 }
681
682 // Move the generated instructions into the ISN_INSTR instruction, then
683 // restore the list of instructions.
684 instr_count = cctx->ctx_instr.ga_len;
685 instr = cctx->ctx_instr.ga_data;
686 instr[instr_count].isn_type = ISN_FINISH;
687
688 cctx->ctx_instr = save_ga;
689 vim_free(isn->isn_arg.string);
690 isn->isn_type = ISN_INSTR;
691 isn->isn_arg.instr = instr;
692 return OK;
693}
694
695/*
696 * Compile the argument expressions.
697 * "arg" points to just after the "(" and is advanced to after the ")"
698 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100699 int
LemonBoyf3b48952022-05-05 13:53:03 +0100700compile_arguments(
701 char_u **arg,
702 cctx_T *cctx,
703 int *argcount,
704 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000705{
706 char_u *p = *arg;
707 char_u *whitep = *arg;
708 int must_end = FALSE;
709 int instr_count;
710
711 for (;;)
712 {
713 if (may_get_next_line(whitep, &p, cctx) == FAIL)
714 goto failret;
715 if (*p == ')')
716 {
717 *arg = p + 1;
718 return OK;
719 }
720 if (must_end)
721 {
722 semsg(_(e_missing_comma_before_argument_str), p);
723 return FAIL;
724 }
725
726 instr_count = cctx->ctx_instr.ga_len;
727 if (compile_expr0(&p, cctx) == FAIL)
728 return FAIL;
729 ++*argcount;
730
LemonBoyf3b48952022-05-05 13:53:03 +0100731 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000732 && cctx->ctx_instr.ga_len == instr_count + 1)
733 {
734 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
735
736 // {skip} argument of searchpair() can be compiled if not empty
737 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100738 compile_string(isn, cctx, 0);
739 }
740 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
741 && cctx->ctx_instr.ga_len == instr_count + 1)
742 {
743 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
744
745 // {sub} argument of substitute() can be compiled if it starts
746 // with \=
747 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100748 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100749 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000750 }
751
752 if (*p != ',' && *skipwhite(p) == ',')
753 {
754 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
755 p = skipwhite(p);
756 }
757 if (*p == ',')
758 {
759 ++p;
760 if (*p != NUL && !VIM_ISWHITE(*p))
761 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
762 }
763 else
764 must_end = TRUE;
765 whitep = p;
766 p = skipwhite(p);
767 }
768failret:
769 emsg(_(e_missing_closing_paren));
770 return FAIL;
771}
772
773/*
774 * Compile a function call: name(arg1, arg2)
775 * "arg" points to "name", "arg + varlen" to the "(".
776 * "argcount_init" is 1 for "value->method()"
777 * Instructions:
778 * EVAL arg1
779 * EVAL arg2
780 * BCALL / DCALL / UCALL
781 */
782 static int
783compile_call(
784 char_u **arg,
785 size_t varlen,
786 cctx_T *cctx,
787 ppconst_T *ppconst,
788 int argcount_init)
789{
790 char_u *name = *arg;
791 char_u *p;
792 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100793 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000794 char_u fname_buf[FLEN_FIXED + 1];
795 char_u *tofree = NULL;
796 int error = FCERR_NONE;
797 ufunc_T *ufunc = NULL;
798 int res = FAIL;
799 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000800 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100801 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000802 imported_T *import;
803
804 if (varlen >= sizeof(namebuf))
805 {
806 semsg(_(e_name_too_long_str), name);
807 return FAIL;
808 }
809 vim_strncpy(namebuf, *arg, varlen);
810
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000811 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000812 if (import != NULL)
813 {
814 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
815 return FAIL;
816 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000817
818 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100819 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000820 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100821 if ((varlen == 3
822 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000823 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
824 {
825 char_u *s = skipwhite(*arg + varlen + 1);
826 typval_T argvars[2];
827 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100828 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000829
830 argvars[0].v_type = VAR_UNKNOWN;
831 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100832 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000833 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100834 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000835 s = skipwhite(s);
836 if (*s == ')' && argvars[0].v_type == VAR_STRING
837 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
838 || !is_has))
839 {
840 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
841
842 *arg = s + 1;
843 argvars[1].v_type = VAR_UNKNOWN;
844 tv->v_type = VAR_NUMBER;
845 tv->vval.v_number = 0;
846 if (is_has)
847 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100848 else if (is_len)
849 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000850 else
851 f_exists(argvars, tv);
852 clear_tv(&argvars[0]);
853 ++ppconst->pp_used;
854 return OK;
855 }
856 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100857 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000858 {
859 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
860 return FAIL;
861 }
862 }
863
864 if (generate_ppconst(cctx, ppconst) == FAIL)
865 return FAIL;
866
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000867 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
868
869 // We handle the "skip" argument of searchpair() and searchpairpos()
870 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100871 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
872 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
873 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
874 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
875 special_fn = CA_SEARCHPAIR;
876 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
877 special_fn = CA_SUBSTITUTE;
878 else
879 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000880
881 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100882 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883 goto theend;
884
885 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
886 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
887 {
888 int idx;
889
890 // builtin function
891 idx = find_internal_func(name);
892 if (idx >= 0)
893 {
894 if (STRCMP(name, "flatten") == 0)
895 {
896 emsg(_(e_cannot_use_flatten_in_vim9_script));
897 goto theend;
898 }
899
900 if (STRCMP(name, "add") == 0 && argcount == 2)
901 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000902 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000903
904 // add() can be compiled to instructions if we know the type
905 if (type->tt_type == VAR_LIST)
906 {
907 // inline "add(list, item)" so that the type can be checked
908 res = generate_LISTAPPEND(cctx);
909 idx = -1;
910 }
911 else if (type->tt_type == VAR_BLOB)
912 {
913 // inline "add(blob, nr)" so that the type can be checked
914 res = generate_BLOBAPPEND(cctx);
915 idx = -1;
916 }
917 }
918
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100919 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
920 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100921 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100922 // May have the "D" or "R" flag, reserve a variable for a
923 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100924 if (get_defer_var_idx(cctx) == 0)
925 idx = -1;
926 }
927
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000928 if (idx >= 0)
929 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
930 }
931 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100932 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000933 goto theend;
934 }
935
Bram Moolenaar62aec932022-01-29 21:45:34 +0000936 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
937
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000938 // An argument or local variable can be a function reference, this
939 // overrules a function name.
940 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
941 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
942 {
943 // If we can find the function by name generate the right call.
944 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000945 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000946 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000947 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000948 if (!func_is_global(ufunc))
949 {
950 res = generate_CALL(cctx, ufunc, argcount);
951 goto theend;
952 }
953 if (!has_g_namespace
954 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
955 {
956 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100957 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000958 goto theend;
959 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000960 }
961 }
962
963 // If the name is a variable, load it and use PCALL.
964 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000965 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000966 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000967 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000968 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
969 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000970 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000971
972 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
973 goto theend;
974 }
975
976 // If we can find a global function by name generate the right call.
977 if (ufunc != NULL)
978 {
979 res = generate_CALL(cctx, ufunc, argcount);
980 goto theend;
981 }
982
983 // A global function may be defined only later. Need to figure out at
984 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000985 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000986 res = generate_UCALL(cctx, name, argcount);
987 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100988 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000989
990theend:
991 vim_free(tofree);
992 return res;
993}
994
995// like NAMESPACE_CHAR but with 'a' and 'l'.
996#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
997
998/*
999 * Find the end of a variable or function name. Unlike find_name_end() this
1000 * does not recognize magic braces.
1001 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
1002 * Return a pointer to just after the name. Equal to "arg" if there is no
1003 * valid name.
1004 */
1005 char_u *
1006to_name_end(char_u *arg, int use_namespace)
1007{
1008 char_u *p;
1009
1010 // Quick check for valid starting character.
1011 if (!eval_isnamec1(*arg))
1012 return arg;
1013
1014 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1015 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1016 // and can be used in slice "[n:]".
1017 if (*p == ':' && (p != arg + 1
1018 || !use_namespace
1019 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1020 break;
1021 return p;
1022}
1023
1024/*
1025 * Like to_name_end() but also skip over a list or dict constant.
1026 * Also accept "<SNR>123_Func".
1027 * This intentionally does not handle line continuation.
1028 */
1029 char_u *
1030to_name_const_end(char_u *arg)
1031{
1032 char_u *p = arg;
1033 typval_T rettv;
1034
1035 if (STRNCMP(p, "<SNR>", 5) == 0)
1036 p = skipdigits(p + 5);
1037 p = to_name_end(p, TRUE);
1038 if (p == arg && *arg == '[')
1039 {
1040
1041 // Can be "[1, 2, 3]->Func()".
1042 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1043 p = arg;
1044 }
1045 return p;
1046}
1047
1048/*
1049 * parse a list: [expr, expr]
1050 * "*arg" points to the '['.
1051 * ppconst->pp_is_const is set if all items are a constant.
1052 */
1053 static int
1054compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1055{
1056 char_u *p = skipwhite(*arg + 1);
1057 char_u *whitep = *arg + 1;
1058 int count = 0;
1059 int is_const;
1060 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001061 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001062
1063 for (;;)
1064 {
1065 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1066 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001067 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001068 return FAIL;
1069 }
1070 if (*p == ',')
1071 {
1072 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1073 return FAIL;
1074 }
1075 if (*p == ']')
1076 {
1077 ++p;
1078 break;
1079 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001080 if (must_end)
1081 {
1082 semsg(_(e_missing_comma_in_list_str), p);
1083 return FAIL;
1084 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001085 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1086 return FAIL;
1087 if (!is_const)
1088 is_all_const = FALSE;
1089 ++count;
1090 if (*p == ',')
1091 {
1092 ++p;
1093 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1094 {
1095 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1096 return FAIL;
1097 }
1098 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001099 else
1100 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001101 whitep = p;
1102 p = skipwhite(p);
1103 }
1104 *arg = p;
1105
1106 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001107 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001108}
1109
1110/*
1111 * Parse a lambda: "(arg, arg) => expr"
1112 * "*arg" points to the '('.
1113 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1114 */
1115 static int
1116compile_lambda(char_u **arg, cctx_T *cctx)
1117{
1118 int r;
1119 typval_T rettv;
1120 ufunc_T *ufunc;
1121 evalarg_T evalarg;
1122
1123 init_evalarg(&evalarg);
1124 evalarg.eval_flags = EVAL_EVALUATE;
1125 evalarg.eval_cctx = cctx;
1126
1127 // Get the funcref in "rettv".
1128 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1129 if (r != OK)
1130 {
1131 clear_evalarg(&evalarg, NULL);
1132 return r;
1133 }
1134
1135 // "rettv" will now be a partial referencing the function.
1136 ufunc = rettv.vval.v_partial->pt_func;
1137 ++ufunc->uf_refcount;
1138 clear_tv(&rettv);
1139
1140 // Compile it here to get the return type. The return type is optional,
1141 // when it's missing use t_unknown. This is recognized in
1142 // compile_return().
1143 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1144 ufunc->uf_ret_type = &t_unknown;
1145 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1146
1147 // When the outer function is compiled for profiling or debugging, the
1148 // lambda may be called without profiling or debugging. Compile it here in
1149 // the right context.
1150 if (cctx->ctx_compile_type == CT_DEBUG
1151#ifdef FEAT_PROFILE
1152 || cctx->ctx_compile_type == CT_PROFILE
1153#endif
1154 )
1155 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1156
Bram Moolenaar139575d2022-03-15 19:29:30 +00001157 // if the outer function is not compiled for debugging or profiling, this
1158 // one might be
1159 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001160 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001161 compiletype_T compile_type = get_compile_type(ufunc);
1162
1163 if (compile_type != CT_NONE)
1164 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001165 }
1166
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001167 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1168 // "*arg" may point into it. Point into the original line to avoid a
1169 // dangling pointer.
1170 if (evalarg.eval_using_cmdline)
1171 {
1172 garray_T *gap = &evalarg.eval_tofree_ga;
1173 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1174
1175 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1176 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001177 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178 }
1179
1180 clear_evalarg(&evalarg, NULL);
1181
1182 if (ufunc->uf_def_status == UF_COMPILED)
1183 {
1184 // The return type will now be known.
1185 set_function_type(ufunc);
1186
1187 // The function reference count will be 1. When the ISN_FUNCREF
1188 // instruction is deleted the reference count is decremented and the
1189 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001190 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001191 }
1192
1193 func_ptr_unref(ufunc);
1194 return FAIL;
1195}
1196
1197/*
1198 * Get a lambda and compile it. Uses Vim9 syntax.
1199 */
1200 int
1201get_lambda_tv_and_compile(
1202 char_u **arg,
1203 typval_T *rettv,
1204 int types_optional,
1205 evalarg_T *evalarg)
1206{
1207 int r;
1208 ufunc_T *ufunc;
1209 int save_sc_version = current_sctx.sc_version;
1210
1211 // Get the funcref in "rettv".
1212 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1213 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1214 current_sctx.sc_version = save_sc_version;
1215 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001216 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001217
1218 // "rettv" will now be a partial referencing the function.
1219 ufunc = rettv->vval.v_partial->pt_func;
1220
1221 // Compile it here to get the return type. The return type is optional,
1222 // when it's missing use t_unknown. This is recognized in
1223 // compile_return().
1224 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1225 ufunc->uf_ret_type = &t_unknown;
1226 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1227
1228 if (ufunc->uf_def_status == UF_COMPILED)
1229 {
1230 // The return type will now be known.
1231 set_function_type(ufunc);
1232 return OK;
1233 }
1234 clear_tv(rettv);
1235 return FAIL;
1236}
1237
1238/*
1239 * parse a dict: {key: val, [key]: val}
1240 * "*arg" points to the '{'.
1241 * ppconst->pp_is_const is set if all item values are a constant.
1242 */
1243 static int
1244compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1245{
1246 garray_T *instr = &cctx->ctx_instr;
1247 int count = 0;
1248 dict_T *d = dict_alloc();
1249 dictitem_T *item;
1250 char_u *whitep = *arg + 1;
1251 char_u *p;
1252 int is_const;
1253 int is_all_const = TRUE; // reset when non-const encountered
1254
1255 if (d == NULL)
1256 return FAIL;
1257 if (generate_ppconst(cctx, ppconst) == FAIL)
1258 return FAIL;
1259 for (;;)
1260 {
1261 char_u *key = NULL;
1262
1263 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1264 {
1265 *arg = NULL;
1266 goto failret;
1267 }
1268
1269 if (**arg == '}')
1270 break;
1271
1272 if (**arg == '[')
1273 {
1274 isn_T *isn;
1275
1276 // {[expr]: value} uses an evaluated key.
1277 *arg = skipwhite(*arg + 1);
1278 if (compile_expr0(arg, cctx) == FAIL)
1279 return FAIL;
1280 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1281 if (isn->isn_type == ISN_PUSHNR)
1282 {
1283 char buf[NUMBUFLEN];
1284
1285 // Convert to string at compile time.
1286 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1287 isn->isn_type = ISN_PUSHS;
1288 isn->isn_arg.string = vim_strsave((char_u *)buf);
1289 }
1290 if (isn->isn_type == ISN_PUSHS)
1291 key = isn->isn_arg.string;
1292 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1293 return FAIL;
1294 *arg = skipwhite(*arg);
1295 if (**arg != ']')
1296 {
1297 emsg(_(e_missing_matching_bracket_after_dict_key));
1298 return FAIL;
1299 }
1300 ++*arg;
1301 }
1302 else
1303 {
1304 // {"name": value},
1305 // {'name': value},
1306 // {name: value} use "name" as a literal key
1307 key = get_literal_key(arg);
1308 if (key == NULL)
1309 return FAIL;
1310 if (generate_PUSHS(cctx, &key) == FAIL)
1311 return FAIL;
1312 }
1313
1314 // Check for duplicate keys, if using string keys.
1315 if (key != NULL)
1316 {
1317 item = dict_find(d, key, -1);
1318 if (item != NULL)
1319 {
dundargocc57b5bc2022-11-02 13:30:51 +00001320 semsg(_(e_duplicate_key_in_dictionary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001321 goto failret;
1322 }
1323 item = dictitem_alloc(key);
1324 if (item != NULL)
1325 {
1326 item->di_tv.v_type = VAR_UNKNOWN;
1327 item->di_tv.v_lock = 0;
1328 if (dict_add(d, item) == FAIL)
1329 dictitem_free(item);
1330 }
1331 }
1332
1333 if (**arg != ':')
1334 {
1335 if (*skipwhite(*arg) == ':')
1336 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1337 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001338 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001339 return FAIL;
1340 }
1341 whitep = *arg + 1;
1342 if (!IS_WHITE_OR_NUL(*whitep))
1343 {
1344 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1345 return FAIL;
1346 }
1347
1348 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1349 {
1350 *arg = NULL;
1351 goto failret;
1352 }
1353
1354 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1355 return FAIL;
1356 if (!is_const)
1357 is_all_const = FALSE;
1358 ++count;
1359
1360 whitep = *arg;
1361 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1362 {
1363 *arg = NULL;
1364 goto failret;
1365 }
1366 if (**arg == '}')
1367 break;
1368 if (**arg != ',')
1369 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001370 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001371 goto failret;
1372 }
1373 if (IS_WHITE_OR_NUL(*whitep))
1374 {
1375 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1376 return FAIL;
1377 }
1378 whitep = *arg + 1;
1379 if (!IS_WHITE_OR_NUL(*whitep))
1380 {
1381 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1382 return FAIL;
1383 }
1384 *arg = skipwhite(whitep);
1385 }
1386
1387 *arg = *arg + 1;
1388
1389 // Allow for following comment, after at least one space.
1390 p = skipwhite(*arg);
1391 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1392 *arg += STRLEN(*arg);
1393
1394 dict_unref(d);
1395 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001396 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001397
1398failret:
1399 if (*arg == NULL)
1400 {
1401 semsg(_(e_missing_dict_end), _("[end of lines]"));
1402 *arg = (char_u *)"";
1403 }
1404 dict_unref(d);
1405 return FAIL;
1406}
1407
1408/*
1409 * Compile "&option".
1410 */
1411 static int
1412compile_get_option(char_u **arg, cctx_T *cctx)
1413{
1414 typval_T rettv;
1415 char_u *start = *arg;
1416 int ret;
1417
1418 // parse the option and get the current value to get the type.
1419 rettv.v_type = VAR_UNKNOWN;
1420 ret = eval_option(arg, &rettv, TRUE);
1421 if (ret == OK)
1422 {
1423 // include the '&' in the name, eval_option() expects it.
1424 char_u *name = vim_strnsave(start, *arg - start);
1425 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1426 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1427
1428 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1429 vim_free(name);
1430 }
1431 clear_tv(&rettv);
1432
1433 return ret;
1434}
1435
1436/*
1437 * Compile "$VAR".
1438 */
1439 static int
1440compile_get_env(char_u **arg, cctx_T *cctx)
1441{
1442 char_u *start = *arg;
1443 int len;
1444 int ret;
1445 char_u *name;
1446
1447 ++*arg;
1448 len = get_env_len(arg);
1449 if (len == 0)
1450 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001451 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001452 return FAIL;
1453 }
1454
1455 // include the '$' in the name, eval_env_var() expects it.
1456 name = vim_strnsave(start, len + 1);
1457 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1458 vim_free(name);
1459 return ret;
1460}
1461
1462/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001463 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001464 */
1465 static int
1466compile_interp_string(char_u **arg, cctx_T *cctx)
1467{
1468 typval_T tv;
1469 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001470 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001471 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001472 int count = 0;
1473 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001474
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001475 // *arg is on the '$' character, move it to the first string character.
1476 ++*arg;
1477 quote = **arg;
1478 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001479
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001480 for (;;)
1481 {
1482 // Get the string up to the matching quote or to a single '{'.
1483 // "arg" is advanced to either the quote or the '{'.
1484 if (quote == '"')
1485 ret = eval_string(arg, &tv, evaluate, TRUE);
1486 else
1487 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1488 if (ret == FAIL)
1489 break;
1490 if (evaluate)
1491 {
1492 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1493 || (**arg != '{' && count == 0))
1494 {
1495 // generate non-empty string or empty string if it's the only
1496 // one
1497 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1498 return FAIL;
1499 tv.vval.v_string = NULL; // don't free it now
1500 ++count;
1501 }
1502 clear_tv(&tv);
1503 }
1504
1505 if (**arg != '{')
1506 {
1507 // found terminating quote
1508 ++*arg;
1509 break;
1510 }
1511
1512 p = compile_one_expr_in_str(*arg, cctx);
1513 if (p == NULL)
1514 {
1515 ret = FAIL;
1516 break;
1517 }
1518 ++count;
1519 *arg = p;
1520 }
LemonBoy2eaef102022-05-06 13:14:50 +01001521
1522 if (ret == FAIL || !evaluate)
1523 return ret;
1524
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001525 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1526 if (count > 1)
1527 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001528
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001529 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001530}
1531
1532/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001533 * Compile "@r".
1534 */
1535 static int
1536compile_get_register(char_u **arg, cctx_T *cctx)
1537{
1538 int ret;
1539
1540 ++*arg;
1541 if (**arg == NUL)
1542 {
1543 semsg(_(e_syntax_error_at_str), *arg - 1);
1544 return FAIL;
1545 }
1546 if (!valid_yank_reg(**arg, FALSE))
1547 {
1548 emsg_invreg(**arg);
1549 return FAIL;
1550 }
1551 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1552 ++*arg;
1553 return ret;
1554}
1555
1556/*
1557 * Apply leading '!', '-' and '+' to constant "rettv".
1558 * When "numeric_only" is TRUE do not apply '!'.
1559 */
1560 static int
1561apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1562{
1563 char_u *p = *end;
1564
1565 // this works from end to start
1566 while (p > start)
1567 {
1568 --p;
1569 if (*p == '-' || *p == '+')
1570 {
1571 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001572 if (rettv->v_type == VAR_FLOAT)
1573 {
1574 if (*p == '-')
1575 rettv->vval.v_float = -rettv->vval.v_float;
1576 }
1577 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001578 {
1579 varnumber_T val;
1580 int error = FALSE;
1581
1582 // tv_get_number_chk() accepts a string, but we don't want that
1583 // here
1584 if (check_not_string(rettv) == FAIL)
1585 return FAIL;
1586 val = tv_get_number_chk(rettv, &error);
1587 clear_tv(rettv);
1588 if (error)
1589 return FAIL;
1590 if (*p == '-')
1591 val = -val;
1592 rettv->v_type = VAR_NUMBER;
1593 rettv->vval.v_number = val;
1594 }
1595 }
1596 else if (numeric_only)
1597 {
1598 ++p;
1599 break;
1600 }
1601 else if (*p == '!')
1602 {
1603 int v = tv2bool(rettv);
1604
1605 // '!' is permissive in the type.
1606 clear_tv(rettv);
1607 rettv->v_type = VAR_BOOL;
1608 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1609 }
1610 }
1611 *end = p;
1612 return OK;
1613}
1614
1615/*
1616 * Recognize v: variables that are constants and set "rettv".
1617 */
1618 static void
1619get_vim_constant(char_u **arg, typval_T *rettv)
1620{
1621 if (STRNCMP(*arg, "v:true", 6) == 0)
1622 {
1623 rettv->v_type = VAR_BOOL;
1624 rettv->vval.v_number = VVAL_TRUE;
1625 *arg += 6;
1626 }
1627 else if (STRNCMP(*arg, "v:false", 7) == 0)
1628 {
1629 rettv->v_type = VAR_BOOL;
1630 rettv->vval.v_number = VVAL_FALSE;
1631 *arg += 7;
1632 }
1633 else if (STRNCMP(*arg, "v:null", 6) == 0)
1634 {
1635 rettv->v_type = VAR_SPECIAL;
1636 rettv->vval.v_number = VVAL_NULL;
1637 *arg += 6;
1638 }
1639 else if (STRNCMP(*arg, "v:none", 6) == 0)
1640 {
1641 rettv->v_type = VAR_SPECIAL;
1642 rettv->vval.v_number = VVAL_NONE;
1643 *arg += 6;
1644 }
1645}
1646
1647 exprtype_T
1648get_compare_type(char_u *p, int *len, int *type_is)
1649{
1650 exprtype_T type = EXPR_UNKNOWN;
1651 int i;
1652
1653 switch (p[0])
1654 {
1655 case '=': if (p[1] == '=')
1656 type = EXPR_EQUAL;
1657 else if (p[1] == '~')
1658 type = EXPR_MATCH;
1659 break;
1660 case '!': if (p[1] == '=')
1661 type = EXPR_NEQUAL;
1662 else if (p[1] == '~')
1663 type = EXPR_NOMATCH;
1664 break;
1665 case '>': if (p[1] != '=')
1666 {
1667 type = EXPR_GREATER;
1668 *len = 1;
1669 }
1670 else
1671 type = EXPR_GEQUAL;
1672 break;
1673 case '<': if (p[1] != '=')
1674 {
1675 type = EXPR_SMALLER;
1676 *len = 1;
1677 }
1678 else
1679 type = EXPR_SEQUAL;
1680 break;
1681 case 'i': if (p[1] == 's')
1682 {
1683 // "is" and "isnot"; but not a prefix of a name
1684 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1685 *len = 5;
1686 i = p[*len];
1687 if (!isalnum(i) && i != '_')
1688 {
1689 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1690 *type_is = TRUE;
1691 }
1692 }
1693 break;
1694 }
1695 return type;
1696}
1697
1698/*
1699 * Skip over an expression, ignoring most errors.
1700 */
1701 void
1702skip_expr_cctx(char_u **arg, cctx_T *cctx)
1703{
1704 evalarg_T evalarg;
1705
1706 init_evalarg(&evalarg);
1707 evalarg.eval_cctx = cctx;
1708 skip_expr(arg, &evalarg);
1709 clear_evalarg(&evalarg, NULL);
1710}
1711
1712/*
1713 * Check that the top of the type stack has a type that can be used as a
1714 * condition. Give an error and return FAIL if not.
1715 */
1716 int
1717bool_on_stack(cctx_T *cctx)
1718{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001719 type_T *type;
1720
Bram Moolenaar078a4612022-01-04 15:17:03 +00001721 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001722 if (type == &t_bool)
1723 return OK;
1724
Bram Moolenaar4913d422022-10-17 13:13:32 +01001725 if (type->tt_type == VAR_ANY
1726 || type->tt_type == VAR_UNKNOWN
1727 || type->tt_type == VAR_NUMBER
1728 || type == &t_number_bool
1729 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001730 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1731 // This requires a runtime type check.
1732 return generate_COND2BOOL(cctx);
1733
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001734 return need_type(type, &t_bool, FALSE, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001735}
1736
1737/*
1738 * Give the "white on both sides" error, taking the operator from "p[len]".
1739 */
1740 void
1741error_white_both(char_u *op, int len)
1742{
1743 char_u buf[10];
1744
1745 vim_strncpy(buf, op, len);
1746 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1747}
1748
1749/*
1750 * Compile code to apply '-', '+' and '!'.
1751 * When "numeric_only" is TRUE do not apply '!'.
1752 */
1753 static int
1754compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1755{
1756 char_u *p = *end;
1757
1758 // this works from end to start
1759 while (p > start)
1760 {
1761 --p;
1762 while (VIM_ISWHITE(*p))
1763 --p;
1764 if (*p == '-' || *p == '+')
1765 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001766 type_T *type = get_type_on_stack(cctx, 0);
1767 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001768 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001769 return FAIL;
1770
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001771 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001772 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1773 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001774 }
1775 else if (numeric_only)
1776 {
1777 ++p;
1778 break;
1779 }
1780 else
1781 {
1782 int invert = *p == '!';
1783
1784 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1785 {
1786 if (p[-1] == '!')
1787 invert = !invert;
1788 --p;
1789 }
1790 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1791 return FAIL;
1792 }
1793 }
1794 *end = p;
1795 return OK;
1796}
1797
1798/*
1799 * Compile "(expression)": recursive!
1800 * Return FAIL/OK.
1801 */
1802 static int
1803compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1804{
1805 int ret;
1806 char_u *p = *arg + 1;
1807
1808 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1809 return FAIL;
1810 if (ppconst->pp_used <= PPSIZE - 10)
1811 {
1812 ret = compile_expr1(arg, cctx, ppconst);
1813 }
1814 else
1815 {
1816 // Not enough space in ppconst, flush constants.
1817 if (generate_ppconst(cctx, ppconst) == FAIL)
1818 return FAIL;
1819 ret = compile_expr0(arg, cctx);
1820 }
1821 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1822 return FAIL;
1823 if (**arg == ')')
1824 ++*arg;
1825 else if (ret == OK)
1826 {
1827 emsg(_(e_missing_closing_paren));
1828 ret = FAIL;
1829 }
1830 return ret;
1831}
1832
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001833static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001834
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001835/*
1836 * Compile whatever comes after "name" or "name()".
1837 * Advances "*arg" only when something was recognized.
1838 */
1839 static int
1840compile_subscript(
1841 char_u **arg,
1842 cctx_T *cctx,
1843 char_u *start_leader,
1844 char_u **end_leader,
1845 ppconst_T *ppconst)
1846{
1847 char_u *name_start = *end_leader;
1848 int keeping_dict = FALSE;
1849
1850 for (;;)
1851 {
1852 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001853 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001854
1855 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1856 {
1857 char_u *next = peek_next_line_from_context(cctx);
1858
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001859 // If a following line starts with "->{", "->(" or "->X" advance to
1860 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001861 // Also if a following line starts with ".x".
1862 if (next != NULL &&
1863 ((next[0] == '-' && next[1] == '>'
1864 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001865 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001866 || ASCII_ISALPHA(*skipwhite(next + 2))))
1867 || (next[0] == '.' && eval_isdictc(next[1]))))
1868 {
1869 next = next_line_from_context(cctx, TRUE);
1870 if (next == NULL)
1871 return FAIL;
1872 *arg = next;
1873 p = skipwhite(*arg);
1874 }
1875 }
1876
1877 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1878 // is not a function call.
1879 if (**arg == '(')
1880 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001881 int argcount = 0;
1882
1883 if (generate_ppconst(cctx, ppconst) == FAIL)
1884 return FAIL;
1885 ppconst->pp_is_const = FALSE;
1886
1887 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001888 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001889
1890 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001891 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001892 return FAIL;
1893 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1894 return FAIL;
1895 if (keeping_dict)
1896 {
1897 keeping_dict = FALSE;
1898 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1899 return FAIL;
1900 }
1901 }
1902 else if (*p == '-' && p[1] == '>')
1903 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001904 char_u *pstart = p;
1905 int alt;
1906 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001907
Bram Moolenaarc7349932022-01-16 20:59:39 +00001908 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001909 if (generate_ppconst(cctx, ppconst) == FAIL)
1910 return FAIL;
1911 ppconst->pp_is_const = FALSE;
1912
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001913 // Apply the '!', '-' and '+' first:
1914 // -1.0->func() works like (-1.0)->func()
1915 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1916 return FAIL;
1917
1918 p += 2;
1919 *arg = skipwhite(p);
1920 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001921
1922 // Three alternatives handled here:
1923 // 1. "base->name(" only a name, use compile_call()
1924 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1925 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001926 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001927 alt = 2;
1928 else
1929 {
1930 // alternative 1 or 3
1931 p = *arg;
1932 if (!eval_isnamec1(*p))
1933 {
1934 semsg(_(e_trailing_characters_str), pstart);
1935 return FAIL;
1936 }
1937 if (ASCII_ISALPHA(*p) && p[1] == ':')
1938 p += 2;
1939 for ( ; eval_isnamec(*p); ++p)
1940 ;
1941 if (*p == '(')
1942 {
1943 // alternative 1
1944 alt = 1;
1945 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1946 return FAIL;
1947 }
1948 else
1949 {
1950 // Must be alternative 3, find the "(". Only works within
1951 // one line.
1952 alt = 3;
1953 paren = vim_strchr(p, '(');
1954 if (paren == NULL)
1955 {
1956 semsg(_(e_missing_parenthesis_str), *arg);
1957 return FAIL;
1958 }
1959 }
1960 }
1961
1962 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001963 {
1964 int argcount = 1;
1965 garray_T *stack = &cctx->ctx_type_stack;
1966 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001967 int expr_isn_start = cctx->ctx_instr.ga_len;
1968 int expr_isn_end;
1969 int arg_isn_count;
1970
Bram Moolenaarc7349932022-01-16 20:59:39 +00001971 if (alt == 2)
1972 {
1973 // Funcref call: list->(Refs[2])(arg)
1974 // or lambda: list->((arg) => expr)(arg)
1975 //
1976 // Fist compile the function expression.
1977 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1978 return FAIL;
1979 }
1980 else
1981 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001982 int fail;
1983 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001984 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001985
Bram Moolenaarc7349932022-01-16 20:59:39 +00001986 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001987
1988 // instead of using LOADG for "import.Func" use PUSHFUNC
1989 ++paren_follows_after_expr;
1990
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001991 // do not look in the next line
1992 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001993
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001994 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001995 || *skipwhite(*arg) != NUL;
1996 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001997 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001998 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001999
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002000 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002001 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002002 if (did_emsg == prev_did_emsg)
2003 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002004 return FAIL;
2005 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002006 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002007
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002008 // Compile the arguments.
2009 if (**arg != '(')
2010 {
2011 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002012 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002013 else
2014 semsg(_(e_missing_parenthesis_str), *arg);
2015 return FAIL;
2016 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002017
2018 // Remember the next instruction index, where the instructions
2019 // for arguments are being written.
2020 expr_isn_end = cctx->ctx_instr.ga_len;
2021
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002022 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002023 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2024 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002025 return FAIL;
2026
2027 // Move the instructions for the arguments to before the
2028 // instructions of the expression and move the type of the
2029 // expression after the argument types. This is what ISN_PCALL
2030 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002031 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2032 if (arg_isn_count > 0)
2033 {
2034 int expr_isn_count = expr_isn_end - expr_isn_start;
2035 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002036 type_T *decl_type;
2037 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002038
2039 if (isn == NULL)
2040 return FAIL;
2041 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2042 + expr_isn_start,
2043 sizeof(isn_T) * expr_isn_count);
2044 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2045 + expr_isn_start,
2046 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2047 sizeof(isn_T) * arg_isn_count);
2048 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2049 + expr_isn_start + arg_isn_count,
2050 isn, sizeof(isn_T) * expr_isn_count);
2051 vim_free(isn);
2052
Bram Moolenaar078a4612022-01-04 15:17:03 +00002053 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2054 type = typep->type_curr;
2055 decl_type = typep->type_decl;
2056 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2057 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2058 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002059 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002060 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2061 typep->type_curr = type;
2062 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002063 }
2064
Bram Moolenaar078a4612022-01-04 15:17:03 +00002065 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002066 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2067 return FAIL;
2068 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002069
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002070 if (keeping_dict)
2071 {
2072 keeping_dict = FALSE;
2073 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2074 return FAIL;
2075 }
2076 }
2077 else if (**arg == '[')
2078 {
2079 int is_slice = FALSE;
2080
2081 // list index: list[123]
2082 // dict member: dict[key]
2083 // string index: text[123]
2084 // blob index: blob[123]
2085 if (generate_ppconst(cctx, ppconst) == FAIL)
2086 return FAIL;
2087 ppconst->pp_is_const = FALSE;
2088
2089 ++p;
2090 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2091 return FAIL;
2092 if (**arg == ':')
2093 {
2094 // missing first index is equal to zero
2095 generate_PUSHNR(cctx, 0);
2096 }
2097 else
2098 {
2099 if (compile_expr0(arg, cctx) == FAIL)
2100 return FAIL;
2101 if (**arg == ':')
2102 {
2103 semsg(_(e_white_space_required_before_and_after_str_at_str),
2104 ":", *arg);
2105 return FAIL;
2106 }
2107 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2108 return FAIL;
2109 *arg = skipwhite(*arg);
2110 }
2111 if (**arg == ':')
2112 {
2113 is_slice = TRUE;
2114 ++*arg;
2115 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2116 {
2117 semsg(_(e_white_space_required_before_and_after_str_at_str),
2118 ":", *arg);
2119 return FAIL;
2120 }
2121 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2122 return FAIL;
2123 if (**arg == ']')
2124 // missing second index is equal to end of string
2125 generate_PUSHNR(cctx, -1);
2126 else
2127 {
2128 if (compile_expr0(arg, cctx) == FAIL)
2129 return FAIL;
2130 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2131 return FAIL;
2132 *arg = skipwhite(*arg);
2133 }
2134 }
2135
2136 if (**arg != ']')
2137 {
2138 emsg(_(e_missing_closing_square_brace));
2139 return FAIL;
2140 }
2141 *arg = *arg + 1;
2142
2143 if (keeping_dict)
2144 {
2145 keeping_dict = FALSE;
2146 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2147 return FAIL;
2148 }
2149 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2150 return FAIL;
2151 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002152 else if (*p == '.'
2153 && (type = get_type_on_stack(cctx, 0)) != &t_unknown
2154 && (type->tt_type == VAR_CLASS || type->tt_type == VAR_OBJECT))
2155 {
2156 // class member: SomeClass.varname
2157 // class method: SomeClass.SomeMethod()
2158 // class constructor: SomeClass.new()
2159 // object member: someObject.varname, this.varname
2160 // object method: someObject.SomeMethod(), this.SomeMethod()
2161 if (compile_class_object_index(cctx, arg, type) == FAIL)
2162 return FAIL;
2163 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002164 else if (*p == '.' && p[1] != '.')
2165 {
2166 // dictionary member: dict.name
2167 if (generate_ppconst(cctx, ppconst) == FAIL)
2168 return FAIL;
2169 ppconst->pp_is_const = FALSE;
2170
2171 *arg = p + 1;
2172 if (IS_WHITE_OR_NUL(**arg))
2173 {
2174 emsg(_(e_missing_name_after_dot));
2175 return FAIL;
2176 }
2177 p = *arg;
2178 if (eval_isdictc(*p))
2179 while (eval_isnamec(*p))
2180 MB_PTR_ADV(p);
2181 if (p == *arg)
2182 {
2183 semsg(_(e_syntax_error_at_str), *arg);
2184 return FAIL;
2185 }
2186 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2187 return FAIL;
2188 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2189 return FAIL;
2190 keeping_dict = TRUE;
2191 *arg = p;
2192 }
2193 else
2194 break;
2195 }
2196
2197 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2198 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002199 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2200 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002201 return FAIL;
2202
2203 return OK;
2204}
2205
2206/*
2207 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2208 * "arg" is advanced until after the expression, skipping white space.
2209 *
2210 * If the value is a constant "ppconst->pp_used" will be non-zero.
2211 * Before instructions are generated, any values in "ppconst" will generated.
2212 *
2213 * This is the compiling equivalent of eval1(), eval2(), etc.
2214 */
2215
2216/*
2217 * number number constant
2218 * 0zFFFFFFFF Blob constant
2219 * "string" string constant
2220 * 'string' literal string constant
2221 * &option-name option value
2222 * @r register contents
2223 * identifier variable value
2224 * function() function call
2225 * $VAR environment variable
2226 * (expression) nested expression
2227 * [expr, expr] List
2228 * {key: val, [key]: val} Dictionary
2229 *
2230 * Also handle:
2231 * ! in front logical NOT
2232 * - in front unary minus
2233 * + in front unary plus (ignored)
2234 * trailing (arg) funcref/partial call
2235 * trailing [] subscript in String or List
2236 * trailing .name entry in Dictionary
2237 * trailing ->name() method call
2238 */
2239 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002240compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002241 char_u **arg,
2242 cctx_T *cctx,
2243 ppconst_T *ppconst)
2244{
2245 char_u *start_leader, *end_leader;
2246 int ret = OK;
2247 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2248 int used_before = ppconst->pp_used;
2249
2250 ppconst->pp_is_const = FALSE;
2251
2252 /*
2253 * Skip '!', '-' and '+' characters. They are handled later.
2254 */
2255 start_leader = *arg;
2256 if (eval_leader(arg, TRUE) == FAIL)
2257 return FAIL;
2258 end_leader = *arg;
2259
2260 rettv->v_type = VAR_UNKNOWN;
2261 switch (**arg)
2262 {
2263 /*
2264 * Number constant.
2265 */
2266 case '0': // also for blob starting with 0z
2267 case '1':
2268 case '2':
2269 case '3':
2270 case '4':
2271 case '5':
2272 case '6':
2273 case '7':
2274 case '8':
2275 case '9':
2276 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2277 return FAIL;
2278 // Apply "-" and "+" just before the number now, right to
2279 // left. Matters especially when "->" follows. Stops at
2280 // '!'.
2281 if (apply_leader(rettv, TRUE,
2282 start_leader, &end_leader) == FAIL)
2283 {
2284 clear_tv(rettv);
2285 return FAIL;
2286 }
2287 break;
2288
2289 /*
2290 * String constant: "string".
2291 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002292 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002293 return FAIL;
2294 break;
2295
2296 /*
2297 * Literal string constant: 'str''ing'.
2298 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002299 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002300 return FAIL;
2301 break;
2302
2303 /*
2304 * Constant Vim variable.
2305 */
2306 case 'v': get_vim_constant(arg, rettv);
2307 ret = NOTDONE;
2308 break;
2309
2310 /*
2311 * "true" constant
2312 */
2313 case 't': if (STRNCMP(*arg, "true", 4) == 0
2314 && !eval_isnamec((*arg)[4]))
2315 {
2316 *arg += 4;
2317 rettv->v_type = VAR_BOOL;
2318 rettv->vval.v_number = VVAL_TRUE;
2319 }
2320 else
2321 ret = NOTDONE;
2322 break;
2323
2324 /*
2325 * "false" constant
2326 */
2327 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2328 && !eval_isnamec((*arg)[5]))
2329 {
2330 *arg += 5;
2331 rettv->v_type = VAR_BOOL;
2332 rettv->vval.v_number = VVAL_FALSE;
2333 }
2334 else
2335 ret = NOTDONE;
2336 break;
2337
2338 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002339 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002340 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002341 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002342 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002343 char_u *p = *arg + 4;
2344 int len;
2345
2346 for (len = 0; eval_isnamec(p[len]); ++len)
2347 ;
2348 ret = handle_predefined(*arg, len + 4, rettv);
2349 if (ret == FAIL)
2350 ret = NOTDONE;
2351 else
2352 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002353 }
2354 else
2355 ret = NOTDONE;
2356 break;
2357
2358 /*
2359 * List: [expr, expr]
2360 */
2361 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2362 return FAIL;
2363 ret = compile_list(arg, cctx, ppconst);
2364 break;
2365
2366 /*
2367 * Dictionary: {'key': val, 'key': val}
2368 */
2369 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2370 return FAIL;
2371 ret = compile_dict(arg, cctx, ppconst);
2372 break;
2373
2374 /*
2375 * Option value: &name
2376 */
2377 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2378 return FAIL;
2379 ret = compile_get_option(arg, cctx);
2380 break;
2381
2382 /*
2383 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002384 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002385 */
2386 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2387 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002388 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2389 ret = compile_interp_string(arg, cctx);
2390 else
2391 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002392 break;
2393
2394 /*
2395 * Register contents: @r.
2396 */
2397 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2398 return FAIL;
2399 ret = compile_get_register(arg, cctx);
2400 break;
2401 /*
2402 * nested expression: (expression).
2403 * lambda: (arg, arg) => expr
2404 * funcref: (arg, arg) => { statement }
2405 */
2406 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2407 ret = compile_lambda(arg, cctx);
2408 if (ret == NOTDONE)
2409 ret = compile_parenthesis(arg, cctx, ppconst);
2410 break;
2411
2412 default: ret = NOTDONE;
2413 break;
2414 }
2415 if (ret == FAIL)
2416 return FAIL;
2417
2418 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2419 {
2420 if (cctx->ctx_skip == SKIP_YES)
2421 clear_tv(rettv);
2422 else
2423 // A constant expression can possibly be handled compile time,
2424 // return the value instead of generating code.
2425 ++ppconst->pp_used;
2426 }
2427 else if (ret == NOTDONE)
2428 {
2429 char_u *p;
2430 int r;
2431
2432 if (!eval_isnamec1(**arg))
2433 {
2434 if (!vim9_bad_comment(*arg))
2435 {
2436 if (ends_excmd(*skipwhite(*arg)))
2437 semsg(_(e_empty_expression_str), *arg);
2438 else
2439 semsg(_(e_name_expected_str), *arg);
2440 }
2441 return FAIL;
2442 }
2443
2444 // "name" or "name()"
2445 p = to_name_end(*arg, TRUE);
2446 if (p - *arg == (size_t)1 && **arg == '_')
2447 {
2448 emsg(_(e_cannot_use_underscore_here));
2449 return FAIL;
2450 }
2451
2452 if (*p == '(')
2453 {
2454 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2455 }
2456 else
2457 {
2458 if (cctx->ctx_skip != SKIP_YES
2459 && generate_ppconst(cctx, ppconst) == FAIL)
2460 return FAIL;
2461 r = compile_load(arg, p, cctx, TRUE, TRUE);
2462 }
2463 if (r == FAIL)
2464 return FAIL;
2465 }
2466
2467 // Handle following "[]", ".member", etc.
2468 // Then deal with prefixed '-', '+' and '!', if not done already.
2469 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2470 ppconst) == FAIL)
2471 return FAIL;
2472 if (ppconst->pp_used > 0)
2473 {
2474 // apply the '!', '-' and '+' before the constant
2475 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2476 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2477 return FAIL;
2478 return OK;
2479 }
2480 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2481 return FAIL;
2482 return OK;
2483}
2484
2485/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002486 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002487 */
2488 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002489compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002490{
2491 type_T *want_type = NULL;
2492
2493 // Recognize <type>
2494 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2495 {
2496 ++*arg;
2497 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2498 if (want_type == NULL)
2499 return FAIL;
2500
2501 if (**arg != '>')
2502 {
2503 if (*skipwhite(*arg) == '>')
2504 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2505 else
2506 emsg(_(e_missing_gt));
2507 return FAIL;
2508 }
2509 ++*arg;
2510 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2511 return FAIL;
2512 }
2513
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002514 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002515 return FAIL;
2516
2517 if (want_type != NULL)
2518 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002519 type_T *actual;
2520 where_T where = WHERE_INIT;
2521
2522 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002523 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002524 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002525 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002526 if (need_type(actual, want_type, FALSE,
2527 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002528 return FAIL;
2529 }
2530 }
2531
2532 return OK;
2533}
2534
2535/*
2536 * * number multiplication
2537 * / number division
2538 * % number modulo
2539 */
2540 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002541compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002542{
2543 char_u *op;
2544 char_u *next;
2545 int ppconst_used = ppconst->pp_used;
2546
2547 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002548 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002549 return FAIL;
2550
2551 /*
2552 * Repeat computing, until no "*", "/" or "%" is following.
2553 */
2554 for (;;)
2555 {
2556 op = may_peek_next_line(cctx, *arg, &next);
2557 if (*op != '*' && *op != '/' && *op != '%')
2558 break;
2559 if (next != NULL)
2560 {
2561 *arg = next_line_from_context(cctx, TRUE);
2562 op = skipwhite(*arg);
2563 }
2564
2565 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2566 {
2567 error_white_both(op, 1);
2568 return FAIL;
2569 }
2570 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2571 return FAIL;
2572
2573 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002574 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002575 return FAIL;
2576
2577 if (ppconst->pp_used == ppconst_used + 2
2578 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2579 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2580 {
2581 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2582 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2583 varnumber_T res = 0;
2584 int failed = FALSE;
2585
2586 // both are numbers: compute the result
2587 switch (*op)
2588 {
2589 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2590 break;
2591 case '/': res = num_divide(tv1->vval.v_number,
2592 tv2->vval.v_number, &failed);
2593 break;
2594 case '%': res = num_modulus(tv1->vval.v_number,
2595 tv2->vval.v_number, &failed);
2596 break;
2597 }
2598 if (failed)
2599 return FAIL;
2600 tv1->vval.v_number = res;
2601 --ppconst->pp_used;
2602 }
2603 else
2604 {
2605 generate_ppconst(cctx, ppconst);
2606 generate_two_op(cctx, op);
2607 }
2608 }
2609
2610 return OK;
2611}
2612
2613/*
2614 * + number addition or list/blobl concatenation
2615 * - number subtraction
2616 * .. string concatenation
2617 */
2618 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002619compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002620{
2621 char_u *op;
2622 char_u *next;
2623 int oplen;
2624 int ppconst_used = ppconst->pp_used;
2625
2626 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002627 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002628 return FAIL;
2629
2630 /*
2631 * Repeat computing, until no "+", "-" or ".." is following.
2632 */
2633 for (;;)
2634 {
2635 op = may_peek_next_line(cctx, *arg, &next);
2636 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2637 break;
2638 if (op[0] == op[1] && *op != '.' && next)
2639 // Finding "++" or "--" on the next line is a separate command.
2640 // But ".." is concatenation.
2641 break;
2642 oplen = (*op == '.' ? 2 : 1);
2643 if (next != NULL)
2644 {
2645 *arg = next_line_from_context(cctx, TRUE);
2646 op = skipwhite(*arg);
2647 }
2648
2649 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2650 {
2651 error_white_both(op, oplen);
2652 return FAIL;
2653 }
2654
2655 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2656 return FAIL;
2657
2658 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002659 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002660 return FAIL;
2661
2662 if (ppconst->pp_used == ppconst_used + 2
2663 && (*op == '.'
2664 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2665 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2666 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2667 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2668 {
2669 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2670 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2671
2672 // concat/subtract/add constant numbers
2673 if (*op == '+')
2674 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2675 else if (*op == '-')
2676 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2677 else
2678 {
2679 // concatenate constant strings
2680 char_u *s1 = tv1->vval.v_string;
2681 char_u *s2 = tv2->vval.v_string;
2682 size_t len1 = STRLEN(s1);
2683
2684 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2685 if (tv1->vval.v_string == NULL)
2686 {
2687 clear_ppconst(ppconst);
2688 return FAIL;
2689 }
2690 mch_memmove(tv1->vval.v_string, s1, len1);
2691 STRCPY(tv1->vval.v_string + len1, s2);
2692 vim_free(s1);
2693 vim_free(s2);
2694 }
2695 --ppconst->pp_used;
2696 }
2697 else
2698 {
2699 generate_ppconst(cctx, ppconst);
2700 ppconst->pp_is_const = FALSE;
2701 if (*op == '.')
2702 {
2703 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2704 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2705 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002706 if (generate_CONCAT(cctx, 2) == FAIL)
2707 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002708 }
2709 else
2710 generate_two_op(cctx, op);
2711 }
2712 }
2713
2714 return OK;
2715}
2716
2717/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002718 * expr6a >> expr6b
2719 * expr6a << expr6b
2720 *
2721 * Produces instructions:
2722 * OPNR bitwise left or right shift
2723 */
2724 static int
2725compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2726{
2727 exprtype_T type = EXPR_UNKNOWN;
2728 char_u *p;
2729 char_u *next;
2730 int len = 2;
2731 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002732 isn_T *isn;
2733
2734 // get the first variable
2735 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2736 return FAIL;
2737
2738 /*
2739 * Repeat computing, until no "+", "-" or ".." is following.
2740 */
2741 for (;;)
2742 {
2743 type = EXPR_UNKNOWN;
2744
2745 p = may_peek_next_line(cctx, *arg, &next);
2746 if (p[0] == '<' && p[1] == '<')
2747 type = EXPR_LSHIFT;
2748 else if (p[0] == '>' && p[1] == '>')
2749 type = EXPR_RSHIFT;
2750
2751 if (type == EXPR_UNKNOWN)
2752 return OK;
2753
2754 // Handle a bitwise left or right shift operator
2755 if (ppconst->pp_used == ppconst_used + 1)
2756 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002757 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002758 {
2759 // left operand should be a number
2760 emsg(_(e_bitshift_ops_must_be_number));
2761 return FAIL;
2762 }
2763 }
2764 else
2765 {
2766 type_T *t = get_type_on_stack(cctx, 0);
2767
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002768 if (need_type(t, &t_number, FALSE, 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002769 {
2770 emsg(_(e_bitshift_ops_must_be_number));
2771 return FAIL;
2772 }
2773 }
2774
2775 if (next != NULL)
2776 {
2777 *arg = next_line_from_context(cctx, TRUE);
2778 p = skipwhite(*arg);
2779 }
2780
2781 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2782 {
2783 error_white_both(p, len);
2784 return FAIL;
2785 }
2786
2787 // get the second variable
2788 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2789 return FAIL;
2790
2791 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2792 return FAIL;
2793
2794 if (ppconst->pp_used == ppconst_used + 2)
2795 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002796 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2797 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2798
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002799 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002800 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2801 {
2802 // right operand should be a positive number
2803 if (tv2->v_type != VAR_NUMBER)
2804 emsg(_(e_bitshift_ops_must_be_number));
2805 else
dundargocc57b5bc2022-11-02 13:30:51 +00002806 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002807 return FAIL;
2808 }
2809
2810 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2811 tv1->vval.v_number = 0;
2812 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002813 tv1->vval.v_number =
2814 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002815 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002816 tv1->vval.v_number =
2817 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002818 clear_tv(tv2);
2819 --ppconst->pp_used;
2820 }
2821 else
2822 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002823 if (need_type(get_type_on_stack(cctx, 0), &t_number, FALSE,
2824 0, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002825 {
2826 emsg(_(e_bitshift_ops_must_be_number));
2827 return FAIL;
2828 }
2829
2830 generate_ppconst(cctx, ppconst);
2831
2832 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2833 if (isn == NULL)
2834 return FAIL;
2835
2836 if (isn != NULL)
2837 isn->isn_arg.op.op_type = type;
2838 }
2839 }
2840
2841 return OK;
2842}
2843
2844/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002845 * expr5a == expr5b
2846 * expr5a =~ expr5b
2847 * expr5a != expr5b
2848 * expr5a !~ expr5b
2849 * expr5a > expr5b
2850 * expr5a >= expr5b
2851 * expr5a < expr5b
2852 * expr5a <= expr5b
2853 * expr5a is expr5b
2854 * expr5a isnot expr5b
2855 *
2856 * Produces instructions:
2857 * EVAL expr5a Push result of "expr5a"
2858 * EVAL expr5b Push result of "expr5b"
2859 * COMPARE one of the compare instructions
2860 */
2861 static int
2862compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2863{
2864 exprtype_T type = EXPR_UNKNOWN;
2865 char_u *p;
2866 char_u *next;
2867 int len = 2;
2868 int type_is = FALSE;
2869 int ppconst_used = ppconst->pp_used;
2870
2871 // get the first variable
2872 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2873 return FAIL;
2874
2875 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002876
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002877 type = get_compare_type(p, &len, &type_is);
2878
2879 /*
2880 * If there is a comparative operator, use it.
2881 */
2882 if (type != EXPR_UNKNOWN)
2883 {
2884 int ic = FALSE; // Default: do not ignore case
2885
2886 if (next != NULL)
2887 {
2888 *arg = next_line_from_context(cctx, TRUE);
2889 p = skipwhite(*arg);
2890 }
2891 if (type_is && (p[len] == '?' || p[len] == '#'))
2892 {
2893 semsg(_(e_invalid_expression_str), *arg);
2894 return FAIL;
2895 }
2896 // extra question mark appended: ignore case
2897 if (p[len] == '?')
2898 {
2899 ic = TRUE;
2900 ++len;
2901 }
2902 // extra '#' appended: match case (ignored)
2903 else if (p[len] == '#')
2904 ++len;
2905 // nothing appended: match case
2906
2907 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2908 {
2909 error_white_both(p, len);
2910 return FAIL;
2911 }
2912
2913 // get the second variable
2914 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2915 return FAIL;
2916
2917 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2918 return FAIL;
2919
2920 if (ppconst->pp_used == ppconst_used + 2)
2921 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002922 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002923 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2924 int ret;
2925
2926 // Both sides are a constant, compute the result now.
2927 // First check for a valid combination of types, this is more
2928 // strict than typval_compare().
2929 if (check_compare_types(type, tv1, tv2) == FAIL)
2930 ret = FAIL;
2931 else
2932 {
2933 ret = typval_compare(tv1, tv2, type, ic);
2934 tv1->v_type = VAR_BOOL;
2935 tv1->vval.v_number = tv1->vval.v_number
2936 ? VVAL_TRUE : VVAL_FALSE;
2937 clear_tv(tv2);
2938 --ppconst->pp_used;
2939 }
2940 return ret;
2941 }
2942
2943 generate_ppconst(cctx, ppconst);
2944 return generate_COMPARE(cctx, type, ic);
2945 }
2946
2947 return OK;
2948}
2949
2950static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2951
2952/*
2953 * Compile || or &&.
2954 */
2955 static int
2956compile_and_or(
2957 char_u **arg,
2958 cctx_T *cctx,
2959 char *op,
2960 ppconst_T *ppconst,
2961 int ppconst_used UNUSED)
2962{
2963 char_u *next;
2964 char_u *p = may_peek_next_line(cctx, *arg, &next);
2965 int opchar = *op;
2966
2967 if (p[0] == opchar && p[1] == opchar)
2968 {
2969 garray_T *instr = &cctx->ctx_instr;
2970 garray_T end_ga;
2971 int save_skip = cctx->ctx_skip;
2972
2973 /*
2974 * Repeat until there is no following "||" or "&&"
2975 */
2976 ga_init2(&end_ga, sizeof(int), 10);
2977 while (p[0] == opchar && p[1] == opchar)
2978 {
2979 long start_lnum = SOURCING_LNUM;
2980 long save_sourcing_lnum;
2981 int start_ctx_lnum = cctx->ctx_lnum;
2982 int save_lnum;
2983 int const_used;
2984 int status;
2985 jumpwhen_T jump_when = opchar == '|'
2986 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2987
2988 if (next != NULL)
2989 {
2990 *arg = next_line_from_context(cctx, TRUE);
2991 p = skipwhite(*arg);
2992 }
2993
2994 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2995 {
2996 semsg(_(e_white_space_required_before_and_after_str_at_str),
2997 op, p);
2998 ga_clear(&end_ga);
2999 return FAIL;
3000 }
3001
3002 save_sourcing_lnum = SOURCING_LNUM;
3003 SOURCING_LNUM = start_lnum;
3004 save_lnum = cctx->ctx_lnum;
3005 cctx->ctx_lnum = start_ctx_lnum;
3006
3007 status = check_ppconst_bool(ppconst);
3008 if (status != FAIL)
3009 {
3010 // Use the last ppconst if possible.
3011 if (ppconst->pp_used > 0)
3012 {
3013 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3014 int is_true = tv2bool(tv);
3015
3016 if ((is_true && opchar == '|')
3017 || (!is_true && opchar == '&'))
3018 {
3019 // For "false && expr" and "true || expr" the "expr"
3020 // does not need to be evaluated.
3021 cctx->ctx_skip = SKIP_YES;
3022 clear_tv(tv);
3023 tv->v_type = VAR_BOOL;
3024 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3025 }
3026 else
3027 {
3028 // For "true && expr" and "false || expr" only "expr"
3029 // needs to be evaluated.
3030 --ppconst->pp_used;
3031 jump_when = JUMP_NEVER;
3032 }
3033 }
3034 else
3035 {
3036 // Every part must evaluate to a bool.
3037 status = bool_on_stack(cctx);
3038 }
3039 }
3040 if (status != FAIL)
3041 status = ga_grow(&end_ga, 1);
3042 cctx->ctx_lnum = save_lnum;
3043 if (status == FAIL)
3044 {
3045 ga_clear(&end_ga);
3046 return FAIL;
3047 }
3048
3049 if (jump_when != JUMP_NEVER)
3050 {
3051 if (cctx->ctx_skip != SKIP_YES)
3052 {
3053 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3054 ++end_ga.ga_len;
3055 }
3056 generate_JUMP(cctx, jump_when, 0);
3057 }
3058
3059 // eval the next expression
3060 SOURCING_LNUM = save_sourcing_lnum;
3061 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3062 {
3063 ga_clear(&end_ga);
3064 return FAIL;
3065 }
3066
3067 const_used = ppconst->pp_used;
3068 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3069 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3070 {
3071 ga_clear(&end_ga);
3072 return FAIL;
3073 }
3074
3075 // "0 || 1" results in true, "1 && 0" results in false.
3076 if (ppconst->pp_used == const_used + 1)
3077 {
3078 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3079
3080 if (tv->v_type == VAR_NUMBER
3081 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3082 {
3083 tv->vval.v_number = tv->vval.v_number == 1
3084 ? VVAL_TRUE : VVAL_FALSE;
3085 tv->v_type = VAR_BOOL;
3086 }
3087 }
3088
3089 p = may_peek_next_line(cctx, *arg, &next);
3090 }
3091
3092 if (check_ppconst_bool(ppconst) == FAIL)
3093 {
3094 ga_clear(&end_ga);
3095 return FAIL;
3096 }
3097
3098 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3099 // Every part must evaluate to a bool.
3100 if (bool_on_stack(cctx) == FAIL)
3101 {
3102 ga_clear(&end_ga);
3103 return FAIL;
3104 }
3105
3106 if (end_ga.ga_len > 0)
3107 {
3108 // Fill in the end label in all jumps.
3109 generate_ppconst(cctx, ppconst);
3110 while (end_ga.ga_len > 0)
3111 {
3112 isn_T *isn;
3113
3114 --end_ga.ga_len;
3115 isn = ((isn_T *)instr->ga_data)
3116 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3117 isn->isn_arg.jump.jump_where = instr->ga_len;
3118 }
3119 }
3120 ga_clear(&end_ga);
3121
3122 cctx->ctx_skip = save_skip;
3123 }
3124
3125 return OK;
3126}
3127
3128/*
3129 * expr4a && expr4a && expr4a logical AND
3130 *
3131 * Produces instructions:
3132 * EVAL expr4a Push result of "expr4a"
3133 * COND2BOOL convert to bool if needed
3134 * JUMP_IF_COND_FALSE end
3135 * EVAL expr4b Push result of "expr4b"
3136 * JUMP_IF_COND_FALSE end
3137 * EVAL expr4c Push result of "expr4c"
3138 * end:
3139 */
3140 static int
3141compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3142{
3143 int ppconst_used = ppconst->pp_used;
3144
3145 // get the first variable
3146 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3147 return FAIL;
3148
3149 // || and && work almost the same
3150 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3151}
3152
3153/*
3154 * expr3a || expr3b || expr3c logical OR
3155 *
3156 * Produces instructions:
3157 * EVAL expr3a Push result of "expr3a"
3158 * COND2BOOL convert to bool if needed
3159 * JUMP_IF_COND_TRUE end
3160 * EVAL expr3b Push result of "expr3b"
3161 * JUMP_IF_COND_TRUE end
3162 * EVAL expr3c Push result of "expr3c"
3163 * end:
3164 */
3165 static int
3166compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3167{
3168 int ppconst_used = ppconst->pp_used;
3169
3170 // eval the first expression
3171 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3172 return FAIL;
3173
3174 // || and && work almost the same
3175 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3176}
3177
3178/*
3179 * Toplevel expression: expr2 ? expr1a : expr1b
3180 * Produces instructions:
3181 * EVAL expr2 Push result of "expr2"
3182 * JUMP_IF_FALSE alt jump if false
3183 * EVAL expr1a
3184 * JUMP_ALWAYS end
3185 * alt: EVAL expr1b
3186 * end:
3187 *
3188 * Toplevel expression: expr2 ?? expr1
3189 * Produces instructions:
3190 * EVAL expr2 Push result of "expr2"
3191 * JUMP_AND_KEEP_IF_TRUE end jump if true
3192 * EVAL expr1
3193 * end:
3194 */
3195 int
3196compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3197{
3198 char_u *p;
3199 int ppconst_used = ppconst->pp_used;
3200 char_u *next;
3201
3202 // Ignore all kinds of errors when not producing code.
3203 if (cctx->ctx_skip == SKIP_YES)
3204 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003205 int prev_did_emsg = did_emsg;
3206
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003207 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003208 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003209 }
3210
3211 // Evaluate the first expression.
3212 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3213 return FAIL;
3214
3215 p = may_peek_next_line(cctx, *arg, &next);
3216 if (*p == '?')
3217 {
3218 int op_falsy = p[1] == '?';
3219 garray_T *instr = &cctx->ctx_instr;
3220 garray_T *stack = &cctx->ctx_type_stack;
3221 int alt_idx = instr->ga_len;
3222 int end_idx = 0;
3223 isn_T *isn;
3224 type_T *type1 = NULL;
3225 int has_const_expr = FALSE;
3226 int const_value = FALSE;
3227 int save_skip = cctx->ctx_skip;
3228
3229 if (next != NULL)
3230 {
3231 *arg = next_line_from_context(cctx, TRUE);
3232 p = skipwhite(*arg);
3233 }
3234
3235 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3236 {
3237 semsg(_(e_white_space_required_before_and_after_str_at_str),
3238 op_falsy ? "??" : "?", p);
3239 return FAIL;
3240 }
3241
3242 if (ppconst->pp_used == ppconst_used + 1)
3243 {
3244 // the condition is a constant, we know whether the ? or the :
3245 // expression is to be evaluated.
3246 has_const_expr = TRUE;
3247 if (op_falsy)
3248 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3249 else
3250 {
3251 int error = FALSE;
3252
3253 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3254 &error);
3255 if (error)
3256 return FAIL;
3257 }
3258 cctx->ctx_skip = save_skip == SKIP_YES ||
3259 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3260
3261 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3262 // "left ?? right" and "left" is truthy: produce "left"
3263 generate_ppconst(cctx, ppconst);
3264 else
3265 {
3266 clear_tv(&ppconst->pp_tv[ppconst_used]);
3267 --ppconst->pp_used;
3268 }
3269 }
3270 else
3271 {
3272 generate_ppconst(cctx, ppconst);
3273 if (op_falsy)
3274 end_idx = instr->ga_len;
3275 generate_JUMP(cctx, op_falsy
3276 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3277 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003278 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003279 }
3280
3281 // evaluate the second expression; any type is accepted
3282 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3283 return FAIL;
3284 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3285 return FAIL;
3286
3287 if (!has_const_expr)
3288 {
3289 generate_ppconst(cctx, ppconst);
3290
3291 if (!op_falsy)
3292 {
3293 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003294 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003295 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003296
3297 end_idx = instr->ga_len;
3298 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3299
3300 // jump here from JUMP_IF_FALSE
3301 isn = ((isn_T *)instr->ga_data) + alt_idx;
3302 isn->isn_arg.jump.jump_where = instr->ga_len;
3303 }
3304 }
3305
3306 if (!op_falsy)
3307 {
3308 // Check for the ":".
3309 p = may_peek_next_line(cctx, *arg, &next);
3310 if (*p != ':')
3311 {
3312 emsg(_(e_missing_colon_after_questionmark));
3313 return FAIL;
3314 }
3315 if (next != NULL)
3316 {
3317 *arg = next_line_from_context(cctx, TRUE);
3318 p = skipwhite(*arg);
3319 }
3320
3321 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3322 {
3323 semsg(_(e_white_space_required_before_and_after_str_at_str),
3324 ":", p);
3325 return FAIL;
3326 }
3327
3328 // evaluate the third expression
3329 if (has_const_expr)
3330 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3331 ? SKIP_YES : SKIP_NOT;
3332 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3333 return FAIL;
3334 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3335 return FAIL;
3336 }
3337
3338 if (!has_const_expr)
3339 {
3340 type_T **typep;
3341
3342 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003343 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003344
3345 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003346 typep = &((((type2_T *)stack->ga_data)
3347 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003348 common_type(type1, *typep, typep, cctx->ctx_type_list);
3349
3350 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3351 isn = ((isn_T *)instr->ga_data) + end_idx;
3352 isn->isn_arg.jump.jump_where = instr->ga_len;
3353 }
3354
3355 cctx->ctx_skip = save_skip;
3356 }
3357 return OK;
3358}
3359
3360/*
3361 * Toplevel expression.
3362 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3363 * Returns OK or FAIL.
3364 */
3365 int
3366compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3367{
3368 ppconst_T ppconst;
3369
3370 CLEAR_FIELD(ppconst);
3371 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3372 {
3373 clear_ppconst(&ppconst);
3374 return FAIL;
3375 }
3376 if (is_const != NULL)
3377 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3378 if (generate_ppconst(cctx, &ppconst) == FAIL)
3379 return FAIL;
3380 return OK;
3381}
3382
3383/*
3384 * Toplevel expression.
3385 */
3386 int
3387compile_expr0(char_u **arg, cctx_T *cctx)
3388{
3389 return compile_expr0_ext(arg, cctx, NULL);
3390}
3391
3392
3393#endif // defined(FEAT_EVAL)