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