blob: 7ec3ee9de760954d4142caef29e543ad5d80283a [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 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000281 ocmember_T *m = &cl->class_obj_members[i];
282 if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000283 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000284 if (*name == '_' && cctx->ctx_ufunc->uf_class != cl)
285 {
Bram Moolenaard505d172022-12-18 21:42:55 +0000286 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +0000287 return FAIL;
288 }
289
Bram Moolenaard505d172022-12-18 21:42:55 +0000290 generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000291
292 *arg = name_end;
293 return OK;
294 }
295 }
296
297 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
298 }
299 else
300 {
301 // TODO: class member
302 emsg("compile_class_object_index(): not handled");
303 }
304
305 return FAIL;
306}
307
308/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000309 * Generate an instruction to load script-local variable "name", without the
310 * leading "s:".
311 * Also finds imported variables.
312 */
313 int
314compile_load_scriptvar(
315 cctx_T *cctx,
316 char_u *name, // variable NUL terminated
317 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100318 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000319{
320 scriptitem_T *si;
321 int idx;
322 imported_T *import;
323
324 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
325 return FAIL;
326 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000327 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000328 if (idx >= 0)
329 {
330 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
331
332 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
333 current_sctx.sc_sid, idx, sv->sv_type);
334 return OK;
335 }
336
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000337 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000338 if (import != NULL)
339 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000340 char_u *p = skipwhite(*end);
341 char_u *exp_name;
342 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100343 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000344 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000345 int done = FALSE;
346 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000347
348 // Need to lookup the member.
349 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000350 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000351 semsg(_(e_expected_dot_after_name_str), start);
352 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000353 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000354 ++p;
355 if (VIM_ISWHITE(*p))
356 {
357 emsg(_(e_no_white_space_allowed_after_dot));
358 return FAIL;
359 }
360
361 // isolate one name
362 exp_name = p;
363 while (eval_isnamec(*p))
364 ++p;
365 cc = *p;
366 *p = NUL;
367
Bram Moolenaard041f422022-01-12 19:54:00 +0000368 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100369 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
370 // "import autoload './dir/script.vim'" or
371 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100372 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100373
374 if (res == OK)
375 {
376 if (si->sn_autoload_prefix != NULL
377 && si->sn_state == SN_STATE_NOT_LOADED)
378 {
379 char_u *auto_name =
380 concat_str(si->sn_autoload_prefix, exp_name);
381
382 // autoload script must be loaded later, access by the autoload
383 // name. If a '(' follows it must be a function. Otherwise we
384 // don't know, it can be "script.Func".
385 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100386 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100387 else
388 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
389 vim_free(auto_name);
390 done = TRUE;
391 }
392 else if (si->sn_import_autoload
393 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100394 {
395 // If a '(' follows it must be a function. Otherwise we don't
396 // know, it can be "script.Func".
397 if (cc == '(' || paren_follows_after_expr)
398 {
399 char_u sid_name[MAX_FUNC_NAME_LEN];
400
401 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100402 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100403 }
404 else
405 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
406 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100407 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100408 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100409 else
410 {
411 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
412 cctx, NULL, TRUE);
413 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100414 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100415
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000416 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000417 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000418 if (done)
419 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000420
421 if (idx < 0)
422 {
423 if (ufunc != NULL)
424 {
425 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100426 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000427 return OK;
428 }
429 return FAIL;
430 }
431
432 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
433 import->imp_sid,
434 idx,
435 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000436 return OK;
437 }
438
Bram Moolenaard0132f42022-05-12 11:05:40 +0100439 // Can only get here if we know "name" is a script variable and not in a
440 // Vim9 script (variable is not in sn_var_vals): old style script.
441 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000442 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000443}
444
445 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000446generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000447{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000448 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000449 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000450
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000451 // Reject a global non-autoload function found without the "g:" prefix.
452 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000453 return FAIL;
454
455 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000456 compile_type = get_compile_type(ufunc);
457 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000458 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000459 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100460 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000461}
462
463/*
464 * Compile a variable name into a load instruction.
465 * "end" points to just after the name.
466 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
467 * When "error" is FALSE do not give an error when not found.
468 */
469 int
470compile_load(
471 char_u **arg,
472 char_u *end_arg,
473 cctx_T *cctx,
474 int is_expr,
475 int error)
476{
477 type_T *type;
478 char_u *name = NULL;
479 char_u *end = end_arg;
480 int res = FAIL;
481 int prev_called_emsg = called_emsg;
482
483 if (*(*arg + 1) == ':')
484 {
485 if (end <= *arg + 2)
486 {
487 isntype_T isn_type;
488
489 // load dictionary of namespace
490 switch (**arg)
491 {
492 case 'g': isn_type = ISN_LOADGDICT; break;
493 case 'w': isn_type = ISN_LOADWDICT; break;
494 case 't': isn_type = ISN_LOADTDICT; break;
495 case 'b': isn_type = ISN_LOADBDICT; break;
496 default:
497 semsg(_(e_namespace_not_supported_str), *arg);
498 goto theend;
499 }
500 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
501 goto theend;
502 res = OK;
503 }
504 else
505 {
506 isntype_T isn_type = ISN_DROP;
507
508 // load namespaced variable
509 name = vim_strnsave(*arg + 2, end - (*arg + 2));
510 if (name == NULL)
511 return FAIL;
512
513 switch (**arg)
514 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100515 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000516 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000517 case 's': if (current_script_is_vim9())
518 {
519 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
520 *arg);
521 vim_free(name);
522 return FAIL;
523 }
Kota Kato948a3892022-08-16 16:09:59 +0100524 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000525 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000526 else
527 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100528 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000529 break;
530 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
531 {
532 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000533 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000534 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000535 else
536 isn_type = ISN_LOADG;
537 }
538 else
539 {
540 isn_type = ISN_LOADAUTO;
541 vim_free(name);
542 name = vim_strnsave(*arg, end - *arg);
543 if (name == NULL)
544 return FAIL;
545 }
546 break;
547 case 'w': isn_type = ISN_LOADW; break;
548 case 't': isn_type = ISN_LOADT; break;
549 case 'b': isn_type = ISN_LOADB; break;
550 default: // cannot happen, just in case
551 semsg(_(e_namespace_not_supported_str), *arg);
552 goto theend;
553 }
554 if (isn_type != ISN_DROP)
555 {
556 // Global, Buffer-local, Window-local and Tabpage-local
557 // variables can be defined later, thus we don't check if it
558 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000559 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000560 }
561 }
562 }
563 else
564 {
565 size_t len = end - *arg;
566 int idx;
567 int gen_load = FALSE;
568 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100569 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100570 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000571
572 name = vim_strnsave(*arg, end - *arg);
573 if (name == NULL)
574 return FAIL;
575
576 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
577 {
578 script_autoload(name, FALSE);
579 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
580 }
581 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
582 == OK)
583 {
584 if (gen_load_outer == 0)
585 gen_load = TRUE;
586 }
587 else
588 {
589 lvar_T lvar;
590
591 if (lookup_local(*arg, len, &lvar, cctx) == OK)
592 {
593 type = lvar.lv_type;
594 idx = lvar.lv_idx;
595 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100596 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000597 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100598 outer_loop_depth = lvar.lv_loop_depth;
599 outer_loop_idx = lvar.lv_loop_idx;
600 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000601 else
602 gen_load = TRUE;
603 }
604 else
605 {
606 // "var" can be script-local even without using "s:" if it
607 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000608 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000609 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100610 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000611
612 // When evaluating an expression and the name starts with an
613 // uppercase letter it can be a user defined function.
614 // generate_funcref() will fail if the function can't be found.
615 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000616 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000617 }
618 }
619 if (gen_load)
620 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
621 if (gen_load_outer > 0)
622 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100623 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
624 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000625 cctx->ctx_outer_used = TRUE;
626 }
627 }
628
629 *arg = end;
630
631theend:
632 if (res == FAIL && error && called_emsg == prev_called_emsg)
633 semsg(_(e_variable_not_found_str), name);
634 vim_free(name);
635 return res;
636}
637
638/*
639 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100640 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000641 * Returns FAIL if compilation fails.
642 */
643 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100644compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000645{
LemonBoyf3b48952022-05-05 13:53:03 +0100646 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000647 garray_T save_ga = cctx->ctx_instr;
648 int expr_res;
649 int trailing_error;
650 int instr_count;
651 isn_T *instr = NULL;
652
653 // Remove the string type from the stack.
654 --cctx->ctx_type_stack.ga_len;
655
656 // Temporarily reset the list of instructions so that the jump labels are
657 // correct.
658 cctx->ctx_instr.ga_len = 0;
659 cctx->ctx_instr.ga_maxlen = 0;
660 cctx->ctx_instr.ga_data = NULL;
661 expr_res = compile_expr0(&s, cctx);
662 s = skipwhite(s);
663 trailing_error = *s != NUL;
664
665 if (expr_res == FAIL || trailing_error
666 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
667 {
668 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000669 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000670 clear_instr_ga(&cctx->ctx_instr);
671 cctx->ctx_instr = save_ga;
672 ++cctx->ctx_type_stack.ga_len;
673 return FAIL;
674 }
675
676 // Move the generated instructions into the ISN_INSTR instruction, then
677 // restore the list of instructions.
678 instr_count = cctx->ctx_instr.ga_len;
679 instr = cctx->ctx_instr.ga_data;
680 instr[instr_count].isn_type = ISN_FINISH;
681
682 cctx->ctx_instr = save_ga;
683 vim_free(isn->isn_arg.string);
684 isn->isn_type = ISN_INSTR;
685 isn->isn_arg.instr = instr;
686 return OK;
687}
688
689/*
690 * Compile the argument expressions.
691 * "arg" points to just after the "(" and is advanced to after the ")"
692 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100693 int
LemonBoyf3b48952022-05-05 13:53:03 +0100694compile_arguments(
695 char_u **arg,
696 cctx_T *cctx,
697 int *argcount,
698 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000699{
700 char_u *p = *arg;
701 char_u *whitep = *arg;
702 int must_end = FALSE;
703 int instr_count;
704
705 for (;;)
706 {
707 if (may_get_next_line(whitep, &p, cctx) == FAIL)
708 goto failret;
709 if (*p == ')')
710 {
711 *arg = p + 1;
712 return OK;
713 }
714 if (must_end)
715 {
716 semsg(_(e_missing_comma_before_argument_str), p);
717 return FAIL;
718 }
719
720 instr_count = cctx->ctx_instr.ga_len;
721 if (compile_expr0(&p, cctx) == FAIL)
722 return FAIL;
723 ++*argcount;
724
LemonBoyf3b48952022-05-05 13:53:03 +0100725 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000726 && cctx->ctx_instr.ga_len == instr_count + 1)
727 {
728 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
729
730 // {skip} argument of searchpair() can be compiled if not empty
731 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100732 compile_string(isn, cctx, 0);
733 }
734 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
735 && cctx->ctx_instr.ga_len == instr_count + 1)
736 {
737 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
738
739 // {sub} argument of substitute() can be compiled if it starts
740 // with \=
741 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100742 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100743 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000744 }
745
746 if (*p != ',' && *skipwhite(p) == ',')
747 {
748 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
749 p = skipwhite(p);
750 }
751 if (*p == ',')
752 {
753 ++p;
754 if (*p != NUL && !VIM_ISWHITE(*p))
755 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
756 }
757 else
758 must_end = TRUE;
759 whitep = p;
760 p = skipwhite(p);
761 }
762failret:
763 emsg(_(e_missing_closing_paren));
764 return FAIL;
765}
766
767/*
768 * Compile a function call: name(arg1, arg2)
769 * "arg" points to "name", "arg + varlen" to the "(".
770 * "argcount_init" is 1 for "value->method()"
771 * Instructions:
772 * EVAL arg1
773 * EVAL arg2
774 * BCALL / DCALL / UCALL
775 */
776 static int
777compile_call(
778 char_u **arg,
779 size_t varlen,
780 cctx_T *cctx,
781 ppconst_T *ppconst,
782 int argcount_init)
783{
784 char_u *name = *arg;
785 char_u *p;
786 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100787 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000788 char_u fname_buf[FLEN_FIXED + 1];
789 char_u *tofree = NULL;
790 int error = FCERR_NONE;
791 ufunc_T *ufunc = NULL;
792 int res = FAIL;
793 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000794 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100795 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000796 imported_T *import;
797
798 if (varlen >= sizeof(namebuf))
799 {
800 semsg(_(e_name_too_long_str), name);
801 return FAIL;
802 }
803 vim_strncpy(namebuf, *arg, varlen);
804
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000805 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000806 if (import != NULL)
807 {
808 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
809 return FAIL;
810 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000811
812 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100813 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000814 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100815 if ((varlen == 3
816 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000817 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
818 {
819 char_u *s = skipwhite(*arg + varlen + 1);
820 typval_T argvars[2];
821 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100822 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000823
824 argvars[0].v_type = VAR_UNKNOWN;
825 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100826 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100828 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000829 s = skipwhite(s);
830 if (*s == ')' && argvars[0].v_type == VAR_STRING
831 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
832 || !is_has))
833 {
834 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
835
836 *arg = s + 1;
837 argvars[1].v_type = VAR_UNKNOWN;
838 tv->v_type = VAR_NUMBER;
839 tv->vval.v_number = 0;
840 if (is_has)
841 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100842 else if (is_len)
843 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000844 else
845 f_exists(argvars, tv);
846 clear_tv(&argvars[0]);
847 ++ppconst->pp_used;
848 return OK;
849 }
850 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100851 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000852 {
853 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
854 return FAIL;
855 }
856 }
857
858 if (generate_ppconst(cctx, ppconst) == FAIL)
859 return FAIL;
860
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000861 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
862
863 // We handle the "skip" argument of searchpair() and searchpairpos()
864 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100865 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
866 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
867 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
868 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
869 special_fn = CA_SEARCHPAIR;
870 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
871 special_fn = CA_SUBSTITUTE;
872 else
873 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000874
875 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100876 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000877 goto theend;
878
879 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
880 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
881 {
882 int idx;
883
884 // builtin function
885 idx = find_internal_func(name);
886 if (idx >= 0)
887 {
888 if (STRCMP(name, "flatten") == 0)
889 {
890 emsg(_(e_cannot_use_flatten_in_vim9_script));
891 goto theend;
892 }
893
894 if (STRCMP(name, "add") == 0 && argcount == 2)
895 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000896 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897
898 // add() can be compiled to instructions if we know the type
899 if (type->tt_type == VAR_LIST)
900 {
901 // inline "add(list, item)" so that the type can be checked
902 res = generate_LISTAPPEND(cctx);
903 idx = -1;
904 }
905 else if (type->tt_type == VAR_BLOB)
906 {
907 // inline "add(blob, nr)" so that the type can be checked
908 res = generate_BLOBAPPEND(cctx);
909 idx = -1;
910 }
911 }
912
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100913 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
914 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100915 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100916 // May have the "D" or "R" flag, reserve a variable for a
917 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100918 if (get_defer_var_idx(cctx) == 0)
919 idx = -1;
920 }
921
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000922 if (idx >= 0)
923 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
924 }
925 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100926 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000927 goto theend;
928 }
929
Bram Moolenaar62aec932022-01-29 21:45:34 +0000930 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
931
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000932 // An argument or local variable can be a function reference, this
933 // overrules a function name.
934 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
935 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
936 {
937 // If we can find the function by name generate the right call.
938 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000939 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000940 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000941 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000942 if (!func_is_global(ufunc))
943 {
944 res = generate_CALL(cctx, ufunc, argcount);
945 goto theend;
946 }
947 if (!has_g_namespace
948 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
949 {
950 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100951 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000952 goto theend;
953 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000954 }
955 }
956
957 // If the name is a variable, load it and use PCALL.
958 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000959 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000960 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000961 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000962 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
963 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000964 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000965
966 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
967 goto theend;
968 }
969
970 // If we can find a global function by name generate the right call.
971 if (ufunc != NULL)
972 {
973 res = generate_CALL(cctx, ufunc, argcount);
974 goto theend;
975 }
976
977 // A global function may be defined only later. Need to figure out at
978 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000979 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000980 res = generate_UCALL(cctx, name, argcount);
981 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100982 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983
984theend:
985 vim_free(tofree);
986 return res;
987}
988
989// like NAMESPACE_CHAR but with 'a' and 'l'.
990#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
991
992/*
993 * Find the end of a variable or function name. Unlike find_name_end() this
994 * does not recognize magic braces.
995 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
996 * Return a pointer to just after the name. Equal to "arg" if there is no
997 * valid name.
998 */
999 char_u *
1000to_name_end(char_u *arg, int use_namespace)
1001{
1002 char_u *p;
1003
1004 // Quick check for valid starting character.
1005 if (!eval_isnamec1(*arg))
1006 return arg;
1007
1008 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1009 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1010 // and can be used in slice "[n:]".
1011 if (*p == ':' && (p != arg + 1
1012 || !use_namespace
1013 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1014 break;
1015 return p;
1016}
1017
1018/*
1019 * Like to_name_end() but also skip over a list or dict constant.
1020 * Also accept "<SNR>123_Func".
1021 * This intentionally does not handle line continuation.
1022 */
1023 char_u *
1024to_name_const_end(char_u *arg)
1025{
1026 char_u *p = arg;
1027 typval_T rettv;
1028
1029 if (STRNCMP(p, "<SNR>", 5) == 0)
1030 p = skipdigits(p + 5);
1031 p = to_name_end(p, TRUE);
1032 if (p == arg && *arg == '[')
1033 {
1034
1035 // Can be "[1, 2, 3]->Func()".
1036 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1037 p = arg;
1038 }
1039 return p;
1040}
1041
1042/*
1043 * parse a list: [expr, expr]
1044 * "*arg" points to the '['.
1045 * ppconst->pp_is_const is set if all items are a constant.
1046 */
1047 static int
1048compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1049{
1050 char_u *p = skipwhite(*arg + 1);
1051 char_u *whitep = *arg + 1;
1052 int count = 0;
1053 int is_const;
1054 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001055 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001056
1057 for (;;)
1058 {
1059 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1060 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001061 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001062 return FAIL;
1063 }
1064 if (*p == ',')
1065 {
1066 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1067 return FAIL;
1068 }
1069 if (*p == ']')
1070 {
1071 ++p;
1072 break;
1073 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001074 if (must_end)
1075 {
1076 semsg(_(e_missing_comma_in_list_str), p);
1077 return FAIL;
1078 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001079 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1080 return FAIL;
1081 if (!is_const)
1082 is_all_const = FALSE;
1083 ++count;
1084 if (*p == ',')
1085 {
1086 ++p;
1087 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1088 {
1089 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1090 return FAIL;
1091 }
1092 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001093 else
1094 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001095 whitep = p;
1096 p = skipwhite(p);
1097 }
1098 *arg = p;
1099
1100 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001101 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001102}
1103
1104/*
1105 * Parse a lambda: "(arg, arg) => expr"
1106 * "*arg" points to the '('.
1107 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1108 */
1109 static int
1110compile_lambda(char_u **arg, cctx_T *cctx)
1111{
1112 int r;
1113 typval_T rettv;
1114 ufunc_T *ufunc;
1115 evalarg_T evalarg;
1116
1117 init_evalarg(&evalarg);
1118 evalarg.eval_flags = EVAL_EVALUATE;
1119 evalarg.eval_cctx = cctx;
1120
1121 // Get the funcref in "rettv".
1122 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1123 if (r != OK)
1124 {
1125 clear_evalarg(&evalarg, NULL);
1126 return r;
1127 }
1128
1129 // "rettv" will now be a partial referencing the function.
1130 ufunc = rettv.vval.v_partial->pt_func;
1131 ++ufunc->uf_refcount;
1132 clear_tv(&rettv);
1133
1134 // Compile it here to get the return type. The return type is optional,
1135 // when it's missing use t_unknown. This is recognized in
1136 // compile_return().
1137 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1138 ufunc->uf_ret_type = &t_unknown;
1139 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1140
1141 // When the outer function is compiled for profiling or debugging, the
1142 // lambda may be called without profiling or debugging. Compile it here in
1143 // the right context.
1144 if (cctx->ctx_compile_type == CT_DEBUG
1145#ifdef FEAT_PROFILE
1146 || cctx->ctx_compile_type == CT_PROFILE
1147#endif
1148 )
1149 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1150
Bram Moolenaar139575d2022-03-15 19:29:30 +00001151 // if the outer function is not compiled for debugging or profiling, this
1152 // one might be
1153 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001154 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001155 compiletype_T compile_type = get_compile_type(ufunc);
1156
1157 if (compile_type != CT_NONE)
1158 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001159 }
1160
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001161 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1162 // "*arg" may point into it. Point into the original line to avoid a
1163 // dangling pointer.
1164 if (evalarg.eval_using_cmdline)
1165 {
1166 garray_T *gap = &evalarg.eval_tofree_ga;
1167 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1168
1169 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1170 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001171 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001172 }
1173
1174 clear_evalarg(&evalarg, NULL);
1175
1176 if (ufunc->uf_def_status == UF_COMPILED)
1177 {
1178 // The return type will now be known.
1179 set_function_type(ufunc);
1180
1181 // The function reference count will be 1. When the ISN_FUNCREF
1182 // instruction is deleted the reference count is decremented and the
1183 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001184 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001185 }
1186
1187 func_ptr_unref(ufunc);
1188 return FAIL;
1189}
1190
1191/*
1192 * Get a lambda and compile it. Uses Vim9 syntax.
1193 */
1194 int
1195get_lambda_tv_and_compile(
1196 char_u **arg,
1197 typval_T *rettv,
1198 int types_optional,
1199 evalarg_T *evalarg)
1200{
1201 int r;
1202 ufunc_T *ufunc;
1203 int save_sc_version = current_sctx.sc_version;
1204
1205 // Get the funcref in "rettv".
1206 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1207 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1208 current_sctx.sc_version = save_sc_version;
1209 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001210 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001211
1212 // "rettv" will now be a partial referencing the function.
1213 ufunc = rettv->vval.v_partial->pt_func;
1214
1215 // Compile it here to get the return type. The return type is optional,
1216 // when it's missing use t_unknown. This is recognized in
1217 // compile_return().
1218 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1219 ufunc->uf_ret_type = &t_unknown;
1220 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1221
1222 if (ufunc->uf_def_status == UF_COMPILED)
1223 {
1224 // The return type will now be known.
1225 set_function_type(ufunc);
1226 return OK;
1227 }
1228 clear_tv(rettv);
1229 return FAIL;
1230}
1231
1232/*
1233 * parse a dict: {key: val, [key]: val}
1234 * "*arg" points to the '{'.
1235 * ppconst->pp_is_const is set if all item values are a constant.
1236 */
1237 static int
1238compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1239{
1240 garray_T *instr = &cctx->ctx_instr;
1241 int count = 0;
1242 dict_T *d = dict_alloc();
1243 dictitem_T *item;
1244 char_u *whitep = *arg + 1;
1245 char_u *p;
1246 int is_const;
1247 int is_all_const = TRUE; // reset when non-const encountered
1248
1249 if (d == NULL)
1250 return FAIL;
1251 if (generate_ppconst(cctx, ppconst) == FAIL)
1252 return FAIL;
1253 for (;;)
1254 {
1255 char_u *key = NULL;
1256
1257 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1258 {
1259 *arg = NULL;
1260 goto failret;
1261 }
1262
1263 if (**arg == '}')
1264 break;
1265
1266 if (**arg == '[')
1267 {
1268 isn_T *isn;
1269
1270 // {[expr]: value} uses an evaluated key.
1271 *arg = skipwhite(*arg + 1);
1272 if (compile_expr0(arg, cctx) == FAIL)
1273 return FAIL;
1274 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1275 if (isn->isn_type == ISN_PUSHNR)
1276 {
1277 char buf[NUMBUFLEN];
1278
1279 // Convert to string at compile time.
1280 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1281 isn->isn_type = ISN_PUSHS;
1282 isn->isn_arg.string = vim_strsave((char_u *)buf);
1283 }
1284 if (isn->isn_type == ISN_PUSHS)
1285 key = isn->isn_arg.string;
1286 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1287 return FAIL;
1288 *arg = skipwhite(*arg);
1289 if (**arg != ']')
1290 {
1291 emsg(_(e_missing_matching_bracket_after_dict_key));
1292 return FAIL;
1293 }
1294 ++*arg;
1295 }
1296 else
1297 {
1298 // {"name": value},
1299 // {'name': value},
1300 // {name: value} use "name" as a literal key
1301 key = get_literal_key(arg);
1302 if (key == NULL)
1303 return FAIL;
1304 if (generate_PUSHS(cctx, &key) == FAIL)
1305 return FAIL;
1306 }
1307
1308 // Check for duplicate keys, if using string keys.
1309 if (key != NULL)
1310 {
1311 item = dict_find(d, key, -1);
1312 if (item != NULL)
1313 {
dundargocc57b5bc2022-11-02 13:30:51 +00001314 semsg(_(e_duplicate_key_in_dictionary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001315 goto failret;
1316 }
1317 item = dictitem_alloc(key);
1318 if (item != NULL)
1319 {
1320 item->di_tv.v_type = VAR_UNKNOWN;
1321 item->di_tv.v_lock = 0;
1322 if (dict_add(d, item) == FAIL)
1323 dictitem_free(item);
1324 }
1325 }
1326
1327 if (**arg != ':')
1328 {
1329 if (*skipwhite(*arg) == ':')
1330 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1331 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001332 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001333 return FAIL;
1334 }
1335 whitep = *arg + 1;
1336 if (!IS_WHITE_OR_NUL(*whitep))
1337 {
1338 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1339 return FAIL;
1340 }
1341
1342 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1343 {
1344 *arg = NULL;
1345 goto failret;
1346 }
1347
1348 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1349 return FAIL;
1350 if (!is_const)
1351 is_all_const = FALSE;
1352 ++count;
1353
1354 whitep = *arg;
1355 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1356 {
1357 *arg = NULL;
1358 goto failret;
1359 }
1360 if (**arg == '}')
1361 break;
1362 if (**arg != ',')
1363 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001364 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001365 goto failret;
1366 }
1367 if (IS_WHITE_OR_NUL(*whitep))
1368 {
1369 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1370 return FAIL;
1371 }
1372 whitep = *arg + 1;
1373 if (!IS_WHITE_OR_NUL(*whitep))
1374 {
1375 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1376 return FAIL;
1377 }
1378 *arg = skipwhite(whitep);
1379 }
1380
1381 *arg = *arg + 1;
1382
1383 // Allow for following comment, after at least one space.
1384 p = skipwhite(*arg);
1385 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1386 *arg += STRLEN(*arg);
1387
1388 dict_unref(d);
1389 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001390 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001391
1392failret:
1393 if (*arg == NULL)
1394 {
1395 semsg(_(e_missing_dict_end), _("[end of lines]"));
1396 *arg = (char_u *)"";
1397 }
1398 dict_unref(d);
1399 return FAIL;
1400}
1401
1402/*
1403 * Compile "&option".
1404 */
1405 static int
1406compile_get_option(char_u **arg, cctx_T *cctx)
1407{
1408 typval_T rettv;
1409 char_u *start = *arg;
1410 int ret;
1411
1412 // parse the option and get the current value to get the type.
1413 rettv.v_type = VAR_UNKNOWN;
1414 ret = eval_option(arg, &rettv, TRUE);
1415 if (ret == OK)
1416 {
1417 // include the '&' in the name, eval_option() expects it.
1418 char_u *name = vim_strnsave(start, *arg - start);
1419 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1420 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1421
1422 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1423 vim_free(name);
1424 }
1425 clear_tv(&rettv);
1426
1427 return ret;
1428}
1429
1430/*
1431 * Compile "$VAR".
1432 */
1433 static int
1434compile_get_env(char_u **arg, cctx_T *cctx)
1435{
1436 char_u *start = *arg;
1437 int len;
1438 int ret;
1439 char_u *name;
1440
1441 ++*arg;
1442 len = get_env_len(arg);
1443 if (len == 0)
1444 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001445 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001446 return FAIL;
1447 }
1448
1449 // include the '$' in the name, eval_env_var() expects it.
1450 name = vim_strnsave(start, len + 1);
1451 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1452 vim_free(name);
1453 return ret;
1454}
1455
1456/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001457 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001458 */
1459 static int
1460compile_interp_string(char_u **arg, cctx_T *cctx)
1461{
1462 typval_T tv;
1463 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001464 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001465 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001466 int count = 0;
1467 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001468
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001469 // *arg is on the '$' character, move it to the first string character.
1470 ++*arg;
1471 quote = **arg;
1472 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001473
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001474 for (;;)
1475 {
1476 // Get the string up to the matching quote or to a single '{'.
1477 // "arg" is advanced to either the quote or the '{'.
1478 if (quote == '"')
1479 ret = eval_string(arg, &tv, evaluate, TRUE);
1480 else
1481 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1482 if (ret == FAIL)
1483 break;
1484 if (evaluate)
1485 {
1486 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1487 || (**arg != '{' && count == 0))
1488 {
1489 // generate non-empty string or empty string if it's the only
1490 // one
1491 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1492 return FAIL;
1493 tv.vval.v_string = NULL; // don't free it now
1494 ++count;
1495 }
1496 clear_tv(&tv);
1497 }
1498
1499 if (**arg != '{')
1500 {
1501 // found terminating quote
1502 ++*arg;
1503 break;
1504 }
1505
1506 p = compile_one_expr_in_str(*arg, cctx);
1507 if (p == NULL)
1508 {
1509 ret = FAIL;
1510 break;
1511 }
1512 ++count;
1513 *arg = p;
1514 }
LemonBoy2eaef102022-05-06 13:14:50 +01001515
1516 if (ret == FAIL || !evaluate)
1517 return ret;
1518
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001519 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1520 if (count > 1)
1521 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001522
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001523 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001524}
1525
1526/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001527 * Compile "@r".
1528 */
1529 static int
1530compile_get_register(char_u **arg, cctx_T *cctx)
1531{
1532 int ret;
1533
1534 ++*arg;
1535 if (**arg == NUL)
1536 {
1537 semsg(_(e_syntax_error_at_str), *arg - 1);
1538 return FAIL;
1539 }
1540 if (!valid_yank_reg(**arg, FALSE))
1541 {
1542 emsg_invreg(**arg);
1543 return FAIL;
1544 }
1545 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1546 ++*arg;
1547 return ret;
1548}
1549
1550/*
1551 * Apply leading '!', '-' and '+' to constant "rettv".
1552 * When "numeric_only" is TRUE do not apply '!'.
1553 */
1554 static int
1555apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1556{
1557 char_u *p = *end;
1558
1559 // this works from end to start
1560 while (p > start)
1561 {
1562 --p;
1563 if (*p == '-' || *p == '+')
1564 {
1565 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001566 if (rettv->v_type == VAR_FLOAT)
1567 {
1568 if (*p == '-')
1569 rettv->vval.v_float = -rettv->vval.v_float;
1570 }
1571 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001572 {
1573 varnumber_T val;
1574 int error = FALSE;
1575
1576 // tv_get_number_chk() accepts a string, but we don't want that
1577 // here
1578 if (check_not_string(rettv) == FAIL)
1579 return FAIL;
1580 val = tv_get_number_chk(rettv, &error);
1581 clear_tv(rettv);
1582 if (error)
1583 return FAIL;
1584 if (*p == '-')
1585 val = -val;
1586 rettv->v_type = VAR_NUMBER;
1587 rettv->vval.v_number = val;
1588 }
1589 }
1590 else if (numeric_only)
1591 {
1592 ++p;
1593 break;
1594 }
1595 else if (*p == '!')
1596 {
1597 int v = tv2bool(rettv);
1598
1599 // '!' is permissive in the type.
1600 clear_tv(rettv);
1601 rettv->v_type = VAR_BOOL;
1602 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1603 }
1604 }
1605 *end = p;
1606 return OK;
1607}
1608
1609/*
1610 * Recognize v: variables that are constants and set "rettv".
1611 */
1612 static void
1613get_vim_constant(char_u **arg, typval_T *rettv)
1614{
1615 if (STRNCMP(*arg, "v:true", 6) == 0)
1616 {
1617 rettv->v_type = VAR_BOOL;
1618 rettv->vval.v_number = VVAL_TRUE;
1619 *arg += 6;
1620 }
1621 else if (STRNCMP(*arg, "v:false", 7) == 0)
1622 {
1623 rettv->v_type = VAR_BOOL;
1624 rettv->vval.v_number = VVAL_FALSE;
1625 *arg += 7;
1626 }
1627 else if (STRNCMP(*arg, "v:null", 6) == 0)
1628 {
1629 rettv->v_type = VAR_SPECIAL;
1630 rettv->vval.v_number = VVAL_NULL;
1631 *arg += 6;
1632 }
1633 else if (STRNCMP(*arg, "v:none", 6) == 0)
1634 {
1635 rettv->v_type = VAR_SPECIAL;
1636 rettv->vval.v_number = VVAL_NONE;
1637 *arg += 6;
1638 }
1639}
1640
1641 exprtype_T
1642get_compare_type(char_u *p, int *len, int *type_is)
1643{
1644 exprtype_T type = EXPR_UNKNOWN;
1645 int i;
1646
1647 switch (p[0])
1648 {
1649 case '=': if (p[1] == '=')
1650 type = EXPR_EQUAL;
1651 else if (p[1] == '~')
1652 type = EXPR_MATCH;
1653 break;
1654 case '!': if (p[1] == '=')
1655 type = EXPR_NEQUAL;
1656 else if (p[1] == '~')
1657 type = EXPR_NOMATCH;
1658 break;
1659 case '>': if (p[1] != '=')
1660 {
1661 type = EXPR_GREATER;
1662 *len = 1;
1663 }
1664 else
1665 type = EXPR_GEQUAL;
1666 break;
1667 case '<': if (p[1] != '=')
1668 {
1669 type = EXPR_SMALLER;
1670 *len = 1;
1671 }
1672 else
1673 type = EXPR_SEQUAL;
1674 break;
1675 case 'i': if (p[1] == 's')
1676 {
1677 // "is" and "isnot"; but not a prefix of a name
1678 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1679 *len = 5;
1680 i = p[*len];
1681 if (!isalnum(i) && i != '_')
1682 {
1683 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1684 *type_is = TRUE;
1685 }
1686 }
1687 break;
1688 }
1689 return type;
1690}
1691
1692/*
1693 * Skip over an expression, ignoring most errors.
1694 */
1695 void
1696skip_expr_cctx(char_u **arg, cctx_T *cctx)
1697{
1698 evalarg_T evalarg;
1699
1700 init_evalarg(&evalarg);
1701 evalarg.eval_cctx = cctx;
1702 skip_expr(arg, &evalarg);
1703 clear_evalarg(&evalarg, NULL);
1704}
1705
1706/*
1707 * Check that the top of the type stack has a type that can be used as a
1708 * condition. Give an error and return FAIL if not.
1709 */
1710 int
1711bool_on_stack(cctx_T *cctx)
1712{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001713 type_T *type;
1714
Bram Moolenaar078a4612022-01-04 15:17:03 +00001715 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001716 if (type == &t_bool)
1717 return OK;
1718
Bram Moolenaar4913d422022-10-17 13:13:32 +01001719 if (type->tt_type == VAR_ANY
1720 || type->tt_type == VAR_UNKNOWN
1721 || type->tt_type == VAR_NUMBER
1722 || type == &t_number_bool
1723 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001724 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1725 // This requires a runtime type check.
1726 return generate_COND2BOOL(cctx);
1727
1728 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1729}
1730
1731/*
1732 * Give the "white on both sides" error, taking the operator from "p[len]".
1733 */
1734 void
1735error_white_both(char_u *op, int len)
1736{
1737 char_u buf[10];
1738
1739 vim_strncpy(buf, op, len);
1740 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1741}
1742
1743/*
1744 * Compile code to apply '-', '+' and '!'.
1745 * When "numeric_only" is TRUE do not apply '!'.
1746 */
1747 static int
1748compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1749{
1750 char_u *p = *end;
1751
1752 // this works from end to start
1753 while (p > start)
1754 {
1755 --p;
1756 while (VIM_ISWHITE(*p))
1757 --p;
1758 if (*p == '-' || *p == '+')
1759 {
1760 int negate = *p == '-';
1761 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001762 type_T *type;
1763
Bram Moolenaar078a4612022-01-04 15:17:03 +00001764 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001765 if (type != &t_float && need_type(type, &t_number,
1766 -1, 0, cctx, FALSE, FALSE) == FAIL)
1767 return FAIL;
1768
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001769 // only '-' has an effect, for '+' we only check the type
1770 if (negate)
1771 {
1772 isn = generate_instr(cctx, ISN_NEGATENR);
1773 if (isn == NULL)
1774 return FAIL;
1775 }
1776 }
1777 else if (numeric_only)
1778 {
1779 ++p;
1780 break;
1781 }
1782 else
1783 {
1784 int invert = *p == '!';
1785
1786 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1787 {
1788 if (p[-1] == '!')
1789 invert = !invert;
1790 --p;
1791 }
1792 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1793 return FAIL;
1794 }
1795 }
1796 *end = p;
1797 return OK;
1798}
1799
1800/*
1801 * Compile "(expression)": recursive!
1802 * Return FAIL/OK.
1803 */
1804 static int
1805compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1806{
1807 int ret;
1808 char_u *p = *arg + 1;
1809
1810 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1811 return FAIL;
1812 if (ppconst->pp_used <= PPSIZE - 10)
1813 {
1814 ret = compile_expr1(arg, cctx, ppconst);
1815 }
1816 else
1817 {
1818 // Not enough space in ppconst, flush constants.
1819 if (generate_ppconst(cctx, ppconst) == FAIL)
1820 return FAIL;
1821 ret = compile_expr0(arg, cctx);
1822 }
1823 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1824 return FAIL;
1825 if (**arg == ')')
1826 ++*arg;
1827 else if (ret == OK)
1828 {
1829 emsg(_(e_missing_closing_paren));
1830 ret = FAIL;
1831 }
1832 return ret;
1833}
1834
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001835static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001836
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001837/*
1838 * Compile whatever comes after "name" or "name()".
1839 * Advances "*arg" only when something was recognized.
1840 */
1841 static int
1842compile_subscript(
1843 char_u **arg,
1844 cctx_T *cctx,
1845 char_u *start_leader,
1846 char_u **end_leader,
1847 ppconst_T *ppconst)
1848{
1849 char_u *name_start = *end_leader;
1850 int keeping_dict = FALSE;
1851
1852 for (;;)
1853 {
1854 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001855 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001856
1857 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1858 {
1859 char_u *next = peek_next_line_from_context(cctx);
1860
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001861 // If a following line starts with "->{", "->(" or "->X" advance to
1862 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001863 // Also if a following line starts with ".x".
1864 if (next != NULL &&
1865 ((next[0] == '-' && next[1] == '>'
1866 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001867 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001868 || ASCII_ISALPHA(*skipwhite(next + 2))))
1869 || (next[0] == '.' && eval_isdictc(next[1]))))
1870 {
1871 next = next_line_from_context(cctx, TRUE);
1872 if (next == NULL)
1873 return FAIL;
1874 *arg = next;
1875 p = skipwhite(*arg);
1876 }
1877 }
1878
1879 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1880 // is not a function call.
1881 if (**arg == '(')
1882 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001883 int argcount = 0;
1884
1885 if (generate_ppconst(cctx, ppconst) == FAIL)
1886 return FAIL;
1887 ppconst->pp_is_const = FALSE;
1888
1889 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001890 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001891
1892 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001893 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001894 return FAIL;
1895 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1896 return FAIL;
1897 if (keeping_dict)
1898 {
1899 keeping_dict = FALSE;
1900 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1901 return FAIL;
1902 }
1903 }
1904 else if (*p == '-' && p[1] == '>')
1905 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001906 char_u *pstart = p;
1907 int alt;
1908 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001909
Bram Moolenaarc7349932022-01-16 20:59:39 +00001910 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001911 if (generate_ppconst(cctx, ppconst) == FAIL)
1912 return FAIL;
1913 ppconst->pp_is_const = FALSE;
1914
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001915 // Apply the '!', '-' and '+' first:
1916 // -1.0->func() works like (-1.0)->func()
1917 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1918 return FAIL;
1919
1920 p += 2;
1921 *arg = skipwhite(p);
1922 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001923
1924 // Three alternatives handled here:
1925 // 1. "base->name(" only a name, use compile_call()
1926 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1927 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001928 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001929 alt = 2;
1930 else
1931 {
1932 // alternative 1 or 3
1933 p = *arg;
1934 if (!eval_isnamec1(*p))
1935 {
1936 semsg(_(e_trailing_characters_str), pstart);
1937 return FAIL;
1938 }
1939 if (ASCII_ISALPHA(*p) && p[1] == ':')
1940 p += 2;
1941 for ( ; eval_isnamec(*p); ++p)
1942 ;
1943 if (*p == '(')
1944 {
1945 // alternative 1
1946 alt = 1;
1947 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1948 return FAIL;
1949 }
1950 else
1951 {
1952 // Must be alternative 3, find the "(". Only works within
1953 // one line.
1954 alt = 3;
1955 paren = vim_strchr(p, '(');
1956 if (paren == NULL)
1957 {
1958 semsg(_(e_missing_parenthesis_str), *arg);
1959 return FAIL;
1960 }
1961 }
1962 }
1963
1964 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001965 {
1966 int argcount = 1;
1967 garray_T *stack = &cctx->ctx_type_stack;
1968 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001969 int expr_isn_start = cctx->ctx_instr.ga_len;
1970 int expr_isn_end;
1971 int arg_isn_count;
1972
Bram Moolenaarc7349932022-01-16 20:59:39 +00001973 if (alt == 2)
1974 {
1975 // Funcref call: list->(Refs[2])(arg)
1976 // or lambda: list->((arg) => expr)(arg)
1977 //
1978 // Fist compile the function expression.
1979 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1980 return FAIL;
1981 }
1982 else
1983 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001984 int fail;
1985 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001986 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001987
Bram Moolenaarc7349932022-01-16 20:59:39 +00001988 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001989
1990 // instead of using LOADG for "import.Func" use PUSHFUNC
1991 ++paren_follows_after_expr;
1992
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001993 // do not look in the next line
1994 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001995
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001996 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001997 || *skipwhite(*arg) != NUL;
1998 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001999 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002000 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00002001
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002002 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00002003 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01002004 if (did_emsg == prev_did_emsg)
2005 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002006 return FAIL;
2007 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002008 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002009
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002010 // Compile the arguments.
2011 if (**arg != '(')
2012 {
2013 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002014 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002015 else
2016 semsg(_(e_missing_parenthesis_str), *arg);
2017 return FAIL;
2018 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002019
2020 // Remember the next instruction index, where the instructions
2021 // for arguments are being written.
2022 expr_isn_end = cctx->ctx_instr.ga_len;
2023
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002024 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002025 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2026 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002027 return FAIL;
2028
2029 // Move the instructions for the arguments to before the
2030 // instructions of the expression and move the type of the
2031 // expression after the argument types. This is what ISN_PCALL
2032 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002033 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2034 if (arg_isn_count > 0)
2035 {
2036 int expr_isn_count = expr_isn_end - expr_isn_start;
2037 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002038 type_T *decl_type;
2039 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002040
2041 if (isn == NULL)
2042 return FAIL;
2043 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2044 + expr_isn_start,
2045 sizeof(isn_T) * expr_isn_count);
2046 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2047 + expr_isn_start,
2048 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2049 sizeof(isn_T) * arg_isn_count);
2050 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2051 + expr_isn_start + arg_isn_count,
2052 isn, sizeof(isn_T) * expr_isn_count);
2053 vim_free(isn);
2054
Bram Moolenaar078a4612022-01-04 15:17:03 +00002055 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2056 type = typep->type_curr;
2057 decl_type = typep->type_decl;
2058 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2059 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2060 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002061 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002062 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2063 typep->type_curr = type;
2064 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002065 }
2066
Bram Moolenaar078a4612022-01-04 15:17:03 +00002067 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002068 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2069 return FAIL;
2070 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002071
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002072 if (keeping_dict)
2073 {
2074 keeping_dict = FALSE;
2075 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2076 return FAIL;
2077 }
2078 }
2079 else if (**arg == '[')
2080 {
2081 int is_slice = FALSE;
2082
2083 // list index: list[123]
2084 // dict member: dict[key]
2085 // string index: text[123]
2086 // blob index: blob[123]
2087 if (generate_ppconst(cctx, ppconst) == FAIL)
2088 return FAIL;
2089 ppconst->pp_is_const = FALSE;
2090
2091 ++p;
2092 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2093 return FAIL;
2094 if (**arg == ':')
2095 {
2096 // missing first index is equal to zero
2097 generate_PUSHNR(cctx, 0);
2098 }
2099 else
2100 {
2101 if (compile_expr0(arg, cctx) == FAIL)
2102 return FAIL;
2103 if (**arg == ':')
2104 {
2105 semsg(_(e_white_space_required_before_and_after_str_at_str),
2106 ":", *arg);
2107 return FAIL;
2108 }
2109 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2110 return FAIL;
2111 *arg = skipwhite(*arg);
2112 }
2113 if (**arg == ':')
2114 {
2115 is_slice = TRUE;
2116 ++*arg;
2117 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2118 {
2119 semsg(_(e_white_space_required_before_and_after_str_at_str),
2120 ":", *arg);
2121 return FAIL;
2122 }
2123 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2124 return FAIL;
2125 if (**arg == ']')
2126 // missing second index is equal to end of string
2127 generate_PUSHNR(cctx, -1);
2128 else
2129 {
2130 if (compile_expr0(arg, cctx) == FAIL)
2131 return FAIL;
2132 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2133 return FAIL;
2134 *arg = skipwhite(*arg);
2135 }
2136 }
2137
2138 if (**arg != ']')
2139 {
2140 emsg(_(e_missing_closing_square_brace));
2141 return FAIL;
2142 }
2143 *arg = *arg + 1;
2144
2145 if (keeping_dict)
2146 {
2147 keeping_dict = FALSE;
2148 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2149 return FAIL;
2150 }
2151 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2152 return FAIL;
2153 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002154 else if (*p == '.'
2155 && (type = get_type_on_stack(cctx, 0)) != &t_unknown
2156 && (type->tt_type == VAR_CLASS || type->tt_type == VAR_OBJECT))
2157 {
2158 // class member: SomeClass.varname
2159 // class method: SomeClass.SomeMethod()
2160 // class constructor: SomeClass.new()
2161 // object member: someObject.varname, this.varname
2162 // object method: someObject.SomeMethod(), this.SomeMethod()
2163 if (compile_class_object_index(cctx, arg, type) == FAIL)
2164 return FAIL;
2165 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002166 else if (*p == '.' && p[1] != '.')
2167 {
2168 // dictionary member: dict.name
2169 if (generate_ppconst(cctx, ppconst) == FAIL)
2170 return FAIL;
2171 ppconst->pp_is_const = FALSE;
2172
2173 *arg = p + 1;
2174 if (IS_WHITE_OR_NUL(**arg))
2175 {
2176 emsg(_(e_missing_name_after_dot));
2177 return FAIL;
2178 }
2179 p = *arg;
2180 if (eval_isdictc(*p))
2181 while (eval_isnamec(*p))
2182 MB_PTR_ADV(p);
2183 if (p == *arg)
2184 {
2185 semsg(_(e_syntax_error_at_str), *arg);
2186 return FAIL;
2187 }
2188 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2189 return FAIL;
2190 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2191 return FAIL;
2192 keeping_dict = TRUE;
2193 *arg = p;
2194 }
2195 else
2196 break;
2197 }
2198
2199 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2200 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002201 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2202 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002203 return FAIL;
2204
2205 return OK;
2206}
2207
2208/*
2209 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2210 * "arg" is advanced until after the expression, skipping white space.
2211 *
2212 * If the value is a constant "ppconst->pp_used" will be non-zero.
2213 * Before instructions are generated, any values in "ppconst" will generated.
2214 *
2215 * This is the compiling equivalent of eval1(), eval2(), etc.
2216 */
2217
2218/*
2219 * number number constant
2220 * 0zFFFFFFFF Blob constant
2221 * "string" string constant
2222 * 'string' literal string constant
2223 * &option-name option value
2224 * @r register contents
2225 * identifier variable value
2226 * function() function call
2227 * $VAR environment variable
2228 * (expression) nested expression
2229 * [expr, expr] List
2230 * {key: val, [key]: val} Dictionary
2231 *
2232 * Also handle:
2233 * ! in front logical NOT
2234 * - in front unary minus
2235 * + in front unary plus (ignored)
2236 * trailing (arg) funcref/partial call
2237 * trailing [] subscript in String or List
2238 * trailing .name entry in Dictionary
2239 * trailing ->name() method call
2240 */
2241 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002242compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002243 char_u **arg,
2244 cctx_T *cctx,
2245 ppconst_T *ppconst)
2246{
2247 char_u *start_leader, *end_leader;
2248 int ret = OK;
2249 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2250 int used_before = ppconst->pp_used;
2251
2252 ppconst->pp_is_const = FALSE;
2253
2254 /*
2255 * Skip '!', '-' and '+' characters. They are handled later.
2256 */
2257 start_leader = *arg;
2258 if (eval_leader(arg, TRUE) == FAIL)
2259 return FAIL;
2260 end_leader = *arg;
2261
2262 rettv->v_type = VAR_UNKNOWN;
2263 switch (**arg)
2264 {
2265 /*
2266 * Number constant.
2267 */
2268 case '0': // also for blob starting with 0z
2269 case '1':
2270 case '2':
2271 case '3':
2272 case '4':
2273 case '5':
2274 case '6':
2275 case '7':
2276 case '8':
2277 case '9':
2278 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2279 return FAIL;
2280 // Apply "-" and "+" just before the number now, right to
2281 // left. Matters especially when "->" follows. Stops at
2282 // '!'.
2283 if (apply_leader(rettv, TRUE,
2284 start_leader, &end_leader) == FAIL)
2285 {
2286 clear_tv(rettv);
2287 return FAIL;
2288 }
2289 break;
2290
2291 /*
2292 * String constant: "string".
2293 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002294 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002295 return FAIL;
2296 break;
2297
2298 /*
2299 * Literal string constant: 'str''ing'.
2300 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002301 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002302 return FAIL;
2303 break;
2304
2305 /*
2306 * Constant Vim variable.
2307 */
2308 case 'v': get_vim_constant(arg, rettv);
2309 ret = NOTDONE;
2310 break;
2311
2312 /*
2313 * "true" constant
2314 */
2315 case 't': if (STRNCMP(*arg, "true", 4) == 0
2316 && !eval_isnamec((*arg)[4]))
2317 {
2318 *arg += 4;
2319 rettv->v_type = VAR_BOOL;
2320 rettv->vval.v_number = VVAL_TRUE;
2321 }
2322 else
2323 ret = NOTDONE;
2324 break;
2325
2326 /*
2327 * "false" constant
2328 */
2329 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2330 && !eval_isnamec((*arg)[5]))
2331 {
2332 *arg += 5;
2333 rettv->v_type = VAR_BOOL;
2334 rettv->vval.v_number = VVAL_FALSE;
2335 }
2336 else
2337 ret = NOTDONE;
2338 break;
2339
2340 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002341 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002342 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002343 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002344 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002345 char_u *p = *arg + 4;
2346 int len;
2347
2348 for (len = 0; eval_isnamec(p[len]); ++len)
2349 ;
2350 ret = handle_predefined(*arg, len + 4, rettv);
2351 if (ret == FAIL)
2352 ret = NOTDONE;
2353 else
2354 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002355 }
2356 else
2357 ret = NOTDONE;
2358 break;
2359
2360 /*
2361 * List: [expr, expr]
2362 */
2363 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2364 return FAIL;
2365 ret = compile_list(arg, cctx, ppconst);
2366 break;
2367
2368 /*
2369 * Dictionary: {'key': val, 'key': val}
2370 */
2371 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2372 return FAIL;
2373 ret = compile_dict(arg, cctx, ppconst);
2374 break;
2375
2376 /*
2377 * Option value: &name
2378 */
2379 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2380 return FAIL;
2381 ret = compile_get_option(arg, cctx);
2382 break;
2383
2384 /*
2385 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002386 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002387 */
2388 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2389 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002390 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2391 ret = compile_interp_string(arg, cctx);
2392 else
2393 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002394 break;
2395
2396 /*
2397 * Register contents: @r.
2398 */
2399 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2400 return FAIL;
2401 ret = compile_get_register(arg, cctx);
2402 break;
2403 /*
2404 * nested expression: (expression).
2405 * lambda: (arg, arg) => expr
2406 * funcref: (arg, arg) => { statement }
2407 */
2408 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2409 ret = compile_lambda(arg, cctx);
2410 if (ret == NOTDONE)
2411 ret = compile_parenthesis(arg, cctx, ppconst);
2412 break;
2413
2414 default: ret = NOTDONE;
2415 break;
2416 }
2417 if (ret == FAIL)
2418 return FAIL;
2419
2420 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2421 {
2422 if (cctx->ctx_skip == SKIP_YES)
2423 clear_tv(rettv);
2424 else
2425 // A constant expression can possibly be handled compile time,
2426 // return the value instead of generating code.
2427 ++ppconst->pp_used;
2428 }
2429 else if (ret == NOTDONE)
2430 {
2431 char_u *p;
2432 int r;
2433
2434 if (!eval_isnamec1(**arg))
2435 {
2436 if (!vim9_bad_comment(*arg))
2437 {
2438 if (ends_excmd(*skipwhite(*arg)))
2439 semsg(_(e_empty_expression_str), *arg);
2440 else
2441 semsg(_(e_name_expected_str), *arg);
2442 }
2443 return FAIL;
2444 }
2445
2446 // "name" or "name()"
2447 p = to_name_end(*arg, TRUE);
2448 if (p - *arg == (size_t)1 && **arg == '_')
2449 {
2450 emsg(_(e_cannot_use_underscore_here));
2451 return FAIL;
2452 }
2453
2454 if (*p == '(')
2455 {
2456 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2457 }
2458 else
2459 {
2460 if (cctx->ctx_skip != SKIP_YES
2461 && generate_ppconst(cctx, ppconst) == FAIL)
2462 return FAIL;
2463 r = compile_load(arg, p, cctx, TRUE, TRUE);
2464 }
2465 if (r == FAIL)
2466 return FAIL;
2467 }
2468
2469 // Handle following "[]", ".member", etc.
2470 // Then deal with prefixed '-', '+' and '!', if not done already.
2471 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2472 ppconst) == FAIL)
2473 return FAIL;
2474 if (ppconst->pp_used > 0)
2475 {
2476 // apply the '!', '-' and '+' before the constant
2477 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2478 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2479 return FAIL;
2480 return OK;
2481 }
2482 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2483 return FAIL;
2484 return OK;
2485}
2486
2487/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002488 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002489 */
2490 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002491compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002492{
2493 type_T *want_type = NULL;
2494
2495 // Recognize <type>
2496 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2497 {
2498 ++*arg;
2499 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2500 if (want_type == NULL)
2501 return FAIL;
2502
2503 if (**arg != '>')
2504 {
2505 if (*skipwhite(*arg) == '>')
2506 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2507 else
2508 emsg(_(e_missing_gt));
2509 return FAIL;
2510 }
2511 ++*arg;
2512 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2513 return FAIL;
2514 }
2515
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002516 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002517 return FAIL;
2518
2519 if (want_type != NULL)
2520 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002521 type_T *actual;
2522 where_T where = WHERE_INIT;
2523
2524 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002525 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002526 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002527 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002528 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2529 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002530 return FAIL;
2531 }
2532 }
2533
2534 return OK;
2535}
2536
2537/*
2538 * * number multiplication
2539 * / number division
2540 * % number modulo
2541 */
2542 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002543compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002544{
2545 char_u *op;
2546 char_u *next;
2547 int ppconst_used = ppconst->pp_used;
2548
2549 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002550 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002551 return FAIL;
2552
2553 /*
2554 * Repeat computing, until no "*", "/" or "%" is following.
2555 */
2556 for (;;)
2557 {
2558 op = may_peek_next_line(cctx, *arg, &next);
2559 if (*op != '*' && *op != '/' && *op != '%')
2560 break;
2561 if (next != NULL)
2562 {
2563 *arg = next_line_from_context(cctx, TRUE);
2564 op = skipwhite(*arg);
2565 }
2566
2567 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2568 {
2569 error_white_both(op, 1);
2570 return FAIL;
2571 }
2572 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2573 return FAIL;
2574
2575 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002576 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002577 return FAIL;
2578
2579 if (ppconst->pp_used == ppconst_used + 2
2580 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2581 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2582 {
2583 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2584 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2585 varnumber_T res = 0;
2586 int failed = FALSE;
2587
2588 // both are numbers: compute the result
2589 switch (*op)
2590 {
2591 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2592 break;
2593 case '/': res = num_divide(tv1->vval.v_number,
2594 tv2->vval.v_number, &failed);
2595 break;
2596 case '%': res = num_modulus(tv1->vval.v_number,
2597 tv2->vval.v_number, &failed);
2598 break;
2599 }
2600 if (failed)
2601 return FAIL;
2602 tv1->vval.v_number = res;
2603 --ppconst->pp_used;
2604 }
2605 else
2606 {
2607 generate_ppconst(cctx, ppconst);
2608 generate_two_op(cctx, op);
2609 }
2610 }
2611
2612 return OK;
2613}
2614
2615/*
2616 * + number addition or list/blobl concatenation
2617 * - number subtraction
2618 * .. string concatenation
2619 */
2620 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002621compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002622{
2623 char_u *op;
2624 char_u *next;
2625 int oplen;
2626 int ppconst_used = ppconst->pp_used;
2627
2628 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002629 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002630 return FAIL;
2631
2632 /*
2633 * Repeat computing, until no "+", "-" or ".." is following.
2634 */
2635 for (;;)
2636 {
2637 op = may_peek_next_line(cctx, *arg, &next);
2638 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2639 break;
2640 if (op[0] == op[1] && *op != '.' && next)
2641 // Finding "++" or "--" on the next line is a separate command.
2642 // But ".." is concatenation.
2643 break;
2644 oplen = (*op == '.' ? 2 : 1);
2645 if (next != NULL)
2646 {
2647 *arg = next_line_from_context(cctx, TRUE);
2648 op = skipwhite(*arg);
2649 }
2650
2651 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2652 {
2653 error_white_both(op, oplen);
2654 return FAIL;
2655 }
2656
2657 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2658 return FAIL;
2659
2660 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002661 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002662 return FAIL;
2663
2664 if (ppconst->pp_used == ppconst_used + 2
2665 && (*op == '.'
2666 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2667 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2668 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2669 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2670 {
2671 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2672 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2673
2674 // concat/subtract/add constant numbers
2675 if (*op == '+')
2676 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2677 else if (*op == '-')
2678 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2679 else
2680 {
2681 // concatenate constant strings
2682 char_u *s1 = tv1->vval.v_string;
2683 char_u *s2 = tv2->vval.v_string;
2684 size_t len1 = STRLEN(s1);
2685
2686 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2687 if (tv1->vval.v_string == NULL)
2688 {
2689 clear_ppconst(ppconst);
2690 return FAIL;
2691 }
2692 mch_memmove(tv1->vval.v_string, s1, len1);
2693 STRCPY(tv1->vval.v_string + len1, s2);
2694 vim_free(s1);
2695 vim_free(s2);
2696 }
2697 --ppconst->pp_used;
2698 }
2699 else
2700 {
2701 generate_ppconst(cctx, ppconst);
2702 ppconst->pp_is_const = FALSE;
2703 if (*op == '.')
2704 {
2705 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2706 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2707 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002708 if (generate_CONCAT(cctx, 2) == FAIL)
2709 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002710 }
2711 else
2712 generate_two_op(cctx, op);
2713 }
2714 }
2715
2716 return OK;
2717}
2718
2719/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002720 * expr6a >> expr6b
2721 * expr6a << expr6b
2722 *
2723 * Produces instructions:
2724 * OPNR bitwise left or right shift
2725 */
2726 static int
2727compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2728{
2729 exprtype_T type = EXPR_UNKNOWN;
2730 char_u *p;
2731 char_u *next;
2732 int len = 2;
2733 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002734 isn_T *isn;
2735
2736 // get the first variable
2737 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2738 return FAIL;
2739
2740 /*
2741 * Repeat computing, until no "+", "-" or ".." is following.
2742 */
2743 for (;;)
2744 {
2745 type = EXPR_UNKNOWN;
2746
2747 p = may_peek_next_line(cctx, *arg, &next);
2748 if (p[0] == '<' && p[1] == '<')
2749 type = EXPR_LSHIFT;
2750 else if (p[0] == '>' && p[1] == '>')
2751 type = EXPR_RSHIFT;
2752
2753 if (type == EXPR_UNKNOWN)
2754 return OK;
2755
2756 // Handle a bitwise left or right shift operator
2757 if (ppconst->pp_used == ppconst_used + 1)
2758 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002759 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002760 {
2761 // left operand should be a number
2762 emsg(_(e_bitshift_ops_must_be_number));
2763 return FAIL;
2764 }
2765 }
2766 else
2767 {
2768 type_T *t = get_type_on_stack(cctx, 0);
2769
2770 if (need_type(t, &t_number, 0, 0, cctx, FALSE, FALSE) == FAIL)
2771 {
2772 emsg(_(e_bitshift_ops_must_be_number));
2773 return FAIL;
2774 }
2775 }
2776
2777 if (next != NULL)
2778 {
2779 *arg = next_line_from_context(cctx, TRUE);
2780 p = skipwhite(*arg);
2781 }
2782
2783 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2784 {
2785 error_white_both(p, len);
2786 return FAIL;
2787 }
2788
2789 // get the second variable
2790 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2791 return FAIL;
2792
2793 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2794 return FAIL;
2795
2796 if (ppconst->pp_used == ppconst_used + 2)
2797 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002798 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2799 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2800
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002801 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002802 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2803 {
2804 // right operand should be a positive number
2805 if (tv2->v_type != VAR_NUMBER)
2806 emsg(_(e_bitshift_ops_must_be_number));
2807 else
dundargocc57b5bc2022-11-02 13:30:51 +00002808 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002809 return FAIL;
2810 }
2811
2812 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2813 tv1->vval.v_number = 0;
2814 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002815 tv1->vval.v_number =
2816 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002817 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002818 tv1->vval.v_number =
2819 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002820 clear_tv(tv2);
2821 --ppconst->pp_used;
2822 }
2823 else
2824 {
2825 if (need_type(get_type_on_stack(cctx, 0), &t_number, 0, 0, cctx,
2826 FALSE, FALSE) == FAIL)
2827 {
2828 emsg(_(e_bitshift_ops_must_be_number));
2829 return FAIL;
2830 }
2831
2832 generate_ppconst(cctx, ppconst);
2833
2834 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2835 if (isn == NULL)
2836 return FAIL;
2837
2838 if (isn != NULL)
2839 isn->isn_arg.op.op_type = type;
2840 }
2841 }
2842
2843 return OK;
2844}
2845
2846/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002847 * expr5a == expr5b
2848 * expr5a =~ expr5b
2849 * expr5a != expr5b
2850 * expr5a !~ expr5b
2851 * expr5a > expr5b
2852 * expr5a >= expr5b
2853 * expr5a < expr5b
2854 * expr5a <= expr5b
2855 * expr5a is expr5b
2856 * expr5a isnot expr5b
2857 *
2858 * Produces instructions:
2859 * EVAL expr5a Push result of "expr5a"
2860 * EVAL expr5b Push result of "expr5b"
2861 * COMPARE one of the compare instructions
2862 */
2863 static int
2864compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2865{
2866 exprtype_T type = EXPR_UNKNOWN;
2867 char_u *p;
2868 char_u *next;
2869 int len = 2;
2870 int type_is = FALSE;
2871 int ppconst_used = ppconst->pp_used;
2872
2873 // get the first variable
2874 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2875 return FAIL;
2876
2877 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002878
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002879 type = get_compare_type(p, &len, &type_is);
2880
2881 /*
2882 * If there is a comparative operator, use it.
2883 */
2884 if (type != EXPR_UNKNOWN)
2885 {
2886 int ic = FALSE; // Default: do not ignore case
2887
2888 if (next != NULL)
2889 {
2890 *arg = next_line_from_context(cctx, TRUE);
2891 p = skipwhite(*arg);
2892 }
2893 if (type_is && (p[len] == '?' || p[len] == '#'))
2894 {
2895 semsg(_(e_invalid_expression_str), *arg);
2896 return FAIL;
2897 }
2898 // extra question mark appended: ignore case
2899 if (p[len] == '?')
2900 {
2901 ic = TRUE;
2902 ++len;
2903 }
2904 // extra '#' appended: match case (ignored)
2905 else if (p[len] == '#')
2906 ++len;
2907 // nothing appended: match case
2908
2909 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2910 {
2911 error_white_both(p, len);
2912 return FAIL;
2913 }
2914
2915 // get the second variable
2916 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2917 return FAIL;
2918
2919 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2920 return FAIL;
2921
2922 if (ppconst->pp_used == ppconst_used + 2)
2923 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002924 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002925 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2926 int ret;
2927
2928 // Both sides are a constant, compute the result now.
2929 // First check for a valid combination of types, this is more
2930 // strict than typval_compare().
2931 if (check_compare_types(type, tv1, tv2) == FAIL)
2932 ret = FAIL;
2933 else
2934 {
2935 ret = typval_compare(tv1, tv2, type, ic);
2936 tv1->v_type = VAR_BOOL;
2937 tv1->vval.v_number = tv1->vval.v_number
2938 ? VVAL_TRUE : VVAL_FALSE;
2939 clear_tv(tv2);
2940 --ppconst->pp_used;
2941 }
2942 return ret;
2943 }
2944
2945 generate_ppconst(cctx, ppconst);
2946 return generate_COMPARE(cctx, type, ic);
2947 }
2948
2949 return OK;
2950}
2951
2952static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2953
2954/*
2955 * Compile || or &&.
2956 */
2957 static int
2958compile_and_or(
2959 char_u **arg,
2960 cctx_T *cctx,
2961 char *op,
2962 ppconst_T *ppconst,
2963 int ppconst_used UNUSED)
2964{
2965 char_u *next;
2966 char_u *p = may_peek_next_line(cctx, *arg, &next);
2967 int opchar = *op;
2968
2969 if (p[0] == opchar && p[1] == opchar)
2970 {
2971 garray_T *instr = &cctx->ctx_instr;
2972 garray_T end_ga;
2973 int save_skip = cctx->ctx_skip;
2974
2975 /*
2976 * Repeat until there is no following "||" or "&&"
2977 */
2978 ga_init2(&end_ga, sizeof(int), 10);
2979 while (p[0] == opchar && p[1] == opchar)
2980 {
2981 long start_lnum = SOURCING_LNUM;
2982 long save_sourcing_lnum;
2983 int start_ctx_lnum = cctx->ctx_lnum;
2984 int save_lnum;
2985 int const_used;
2986 int status;
2987 jumpwhen_T jump_when = opchar == '|'
2988 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2989
2990 if (next != NULL)
2991 {
2992 *arg = next_line_from_context(cctx, TRUE);
2993 p = skipwhite(*arg);
2994 }
2995
2996 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2997 {
2998 semsg(_(e_white_space_required_before_and_after_str_at_str),
2999 op, p);
3000 ga_clear(&end_ga);
3001 return FAIL;
3002 }
3003
3004 save_sourcing_lnum = SOURCING_LNUM;
3005 SOURCING_LNUM = start_lnum;
3006 save_lnum = cctx->ctx_lnum;
3007 cctx->ctx_lnum = start_ctx_lnum;
3008
3009 status = check_ppconst_bool(ppconst);
3010 if (status != FAIL)
3011 {
3012 // Use the last ppconst if possible.
3013 if (ppconst->pp_used > 0)
3014 {
3015 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3016 int is_true = tv2bool(tv);
3017
3018 if ((is_true && opchar == '|')
3019 || (!is_true && opchar == '&'))
3020 {
3021 // For "false && expr" and "true || expr" the "expr"
3022 // does not need to be evaluated.
3023 cctx->ctx_skip = SKIP_YES;
3024 clear_tv(tv);
3025 tv->v_type = VAR_BOOL;
3026 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3027 }
3028 else
3029 {
3030 // For "true && expr" and "false || expr" only "expr"
3031 // needs to be evaluated.
3032 --ppconst->pp_used;
3033 jump_when = JUMP_NEVER;
3034 }
3035 }
3036 else
3037 {
3038 // Every part must evaluate to a bool.
3039 status = bool_on_stack(cctx);
3040 }
3041 }
3042 if (status != FAIL)
3043 status = ga_grow(&end_ga, 1);
3044 cctx->ctx_lnum = save_lnum;
3045 if (status == FAIL)
3046 {
3047 ga_clear(&end_ga);
3048 return FAIL;
3049 }
3050
3051 if (jump_when != JUMP_NEVER)
3052 {
3053 if (cctx->ctx_skip != SKIP_YES)
3054 {
3055 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3056 ++end_ga.ga_len;
3057 }
3058 generate_JUMP(cctx, jump_when, 0);
3059 }
3060
3061 // eval the next expression
3062 SOURCING_LNUM = save_sourcing_lnum;
3063 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3064 {
3065 ga_clear(&end_ga);
3066 return FAIL;
3067 }
3068
3069 const_used = ppconst->pp_used;
3070 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3071 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3072 {
3073 ga_clear(&end_ga);
3074 return FAIL;
3075 }
3076
3077 // "0 || 1" results in true, "1 && 0" results in false.
3078 if (ppconst->pp_used == const_used + 1)
3079 {
3080 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3081
3082 if (tv->v_type == VAR_NUMBER
3083 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3084 {
3085 tv->vval.v_number = tv->vval.v_number == 1
3086 ? VVAL_TRUE : VVAL_FALSE;
3087 tv->v_type = VAR_BOOL;
3088 }
3089 }
3090
3091 p = may_peek_next_line(cctx, *arg, &next);
3092 }
3093
3094 if (check_ppconst_bool(ppconst) == FAIL)
3095 {
3096 ga_clear(&end_ga);
3097 return FAIL;
3098 }
3099
3100 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3101 // Every part must evaluate to a bool.
3102 if (bool_on_stack(cctx) == FAIL)
3103 {
3104 ga_clear(&end_ga);
3105 return FAIL;
3106 }
3107
3108 if (end_ga.ga_len > 0)
3109 {
3110 // Fill in the end label in all jumps.
3111 generate_ppconst(cctx, ppconst);
3112 while (end_ga.ga_len > 0)
3113 {
3114 isn_T *isn;
3115
3116 --end_ga.ga_len;
3117 isn = ((isn_T *)instr->ga_data)
3118 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3119 isn->isn_arg.jump.jump_where = instr->ga_len;
3120 }
3121 }
3122 ga_clear(&end_ga);
3123
3124 cctx->ctx_skip = save_skip;
3125 }
3126
3127 return OK;
3128}
3129
3130/*
3131 * expr4a && expr4a && expr4a logical AND
3132 *
3133 * Produces instructions:
3134 * EVAL expr4a Push result of "expr4a"
3135 * COND2BOOL convert to bool if needed
3136 * JUMP_IF_COND_FALSE end
3137 * EVAL expr4b Push result of "expr4b"
3138 * JUMP_IF_COND_FALSE end
3139 * EVAL expr4c Push result of "expr4c"
3140 * end:
3141 */
3142 static int
3143compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3144{
3145 int ppconst_used = ppconst->pp_used;
3146
3147 // get the first variable
3148 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3149 return FAIL;
3150
3151 // || and && work almost the same
3152 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3153}
3154
3155/*
3156 * expr3a || expr3b || expr3c logical OR
3157 *
3158 * Produces instructions:
3159 * EVAL expr3a Push result of "expr3a"
3160 * COND2BOOL convert to bool if needed
3161 * JUMP_IF_COND_TRUE end
3162 * EVAL expr3b Push result of "expr3b"
3163 * JUMP_IF_COND_TRUE end
3164 * EVAL expr3c Push result of "expr3c"
3165 * end:
3166 */
3167 static int
3168compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3169{
3170 int ppconst_used = ppconst->pp_used;
3171
3172 // eval the first expression
3173 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3174 return FAIL;
3175
3176 // || and && work almost the same
3177 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3178}
3179
3180/*
3181 * Toplevel expression: expr2 ? expr1a : expr1b
3182 * Produces instructions:
3183 * EVAL expr2 Push result of "expr2"
3184 * JUMP_IF_FALSE alt jump if false
3185 * EVAL expr1a
3186 * JUMP_ALWAYS end
3187 * alt: EVAL expr1b
3188 * end:
3189 *
3190 * Toplevel expression: expr2 ?? expr1
3191 * Produces instructions:
3192 * EVAL expr2 Push result of "expr2"
3193 * JUMP_AND_KEEP_IF_TRUE end jump if true
3194 * EVAL expr1
3195 * end:
3196 */
3197 int
3198compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3199{
3200 char_u *p;
3201 int ppconst_used = ppconst->pp_used;
3202 char_u *next;
3203
3204 // Ignore all kinds of errors when not producing code.
3205 if (cctx->ctx_skip == SKIP_YES)
3206 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003207 int prev_did_emsg = did_emsg;
3208
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003209 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003210 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003211 }
3212
3213 // Evaluate the first expression.
3214 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3215 return FAIL;
3216
3217 p = may_peek_next_line(cctx, *arg, &next);
3218 if (*p == '?')
3219 {
3220 int op_falsy = p[1] == '?';
3221 garray_T *instr = &cctx->ctx_instr;
3222 garray_T *stack = &cctx->ctx_type_stack;
3223 int alt_idx = instr->ga_len;
3224 int end_idx = 0;
3225 isn_T *isn;
3226 type_T *type1 = NULL;
3227 int has_const_expr = FALSE;
3228 int const_value = FALSE;
3229 int save_skip = cctx->ctx_skip;
3230
3231 if (next != NULL)
3232 {
3233 *arg = next_line_from_context(cctx, TRUE);
3234 p = skipwhite(*arg);
3235 }
3236
3237 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3238 {
3239 semsg(_(e_white_space_required_before_and_after_str_at_str),
3240 op_falsy ? "??" : "?", p);
3241 return FAIL;
3242 }
3243
3244 if (ppconst->pp_used == ppconst_used + 1)
3245 {
3246 // the condition is a constant, we know whether the ? or the :
3247 // expression is to be evaluated.
3248 has_const_expr = TRUE;
3249 if (op_falsy)
3250 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3251 else
3252 {
3253 int error = FALSE;
3254
3255 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3256 &error);
3257 if (error)
3258 return FAIL;
3259 }
3260 cctx->ctx_skip = save_skip == SKIP_YES ||
3261 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3262
3263 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3264 // "left ?? right" and "left" is truthy: produce "left"
3265 generate_ppconst(cctx, ppconst);
3266 else
3267 {
3268 clear_tv(&ppconst->pp_tv[ppconst_used]);
3269 --ppconst->pp_used;
3270 }
3271 }
3272 else
3273 {
3274 generate_ppconst(cctx, ppconst);
3275 if (op_falsy)
3276 end_idx = instr->ga_len;
3277 generate_JUMP(cctx, op_falsy
3278 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3279 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003280 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003281 }
3282
3283 // evaluate the second expression; any type is accepted
3284 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3285 return FAIL;
3286 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3287 return FAIL;
3288
3289 if (!has_const_expr)
3290 {
3291 generate_ppconst(cctx, ppconst);
3292
3293 if (!op_falsy)
3294 {
3295 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003296 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003297 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003298
3299 end_idx = instr->ga_len;
3300 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3301
3302 // jump here from JUMP_IF_FALSE
3303 isn = ((isn_T *)instr->ga_data) + alt_idx;
3304 isn->isn_arg.jump.jump_where = instr->ga_len;
3305 }
3306 }
3307
3308 if (!op_falsy)
3309 {
3310 // Check for the ":".
3311 p = may_peek_next_line(cctx, *arg, &next);
3312 if (*p != ':')
3313 {
3314 emsg(_(e_missing_colon_after_questionmark));
3315 return FAIL;
3316 }
3317 if (next != NULL)
3318 {
3319 *arg = next_line_from_context(cctx, TRUE);
3320 p = skipwhite(*arg);
3321 }
3322
3323 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3324 {
3325 semsg(_(e_white_space_required_before_and_after_str_at_str),
3326 ":", p);
3327 return FAIL;
3328 }
3329
3330 // evaluate the third expression
3331 if (has_const_expr)
3332 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3333 ? SKIP_YES : SKIP_NOT;
3334 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3335 return FAIL;
3336 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3337 return FAIL;
3338 }
3339
3340 if (!has_const_expr)
3341 {
3342 type_T **typep;
3343
3344 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003345 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003346
3347 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003348 typep = &((((type2_T *)stack->ga_data)
3349 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003350 common_type(type1, *typep, typep, cctx->ctx_type_list);
3351
3352 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3353 isn = ((isn_T *)instr->ga_data) + end_idx;
3354 isn->isn_arg.jump.jump_where = instr->ga_len;
3355 }
3356
3357 cctx->ctx_skip = save_skip;
3358 }
3359 return OK;
3360}
3361
3362/*
3363 * Toplevel expression.
3364 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3365 * Returns OK or FAIL.
3366 */
3367 int
3368compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3369{
3370 ppconst_T ppconst;
3371
3372 CLEAR_FIELD(ppconst);
3373 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3374 {
3375 clear_ppconst(&ppconst);
3376 return FAIL;
3377 }
3378 if (is_const != NULL)
3379 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3380 if (generate_ppconst(cctx, &ppconst) == FAIL)
3381 return FAIL;
3382 return OK;
3383}
3384
3385/*
3386 * Toplevel expression.
3387 */
3388 int
3389compile_expr0(char_u **arg, cctx_T *cctx)
3390{
3391 return compile_expr0_ext(arg, cctx, NULL);
3392}
3393
3394
3395#endif // defined(FEAT_EVAL)