blob: b4e201eb5b2af190b9f696dee9b3a01ecc93f33e [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 {
Bram Moolenaar73ade492022-12-27 20:54:41 +00001760 type_T *type = get_type_on_stack(cctx, 0);
1761 if (type->tt_type != VAR_FLOAT && need_type(type, &t_number,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001762 -1, 0, cctx, FALSE, FALSE) == FAIL)
1763 return FAIL;
1764
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001765 // only '-' has an effect, for '+' we only check the type
Bram Moolenaar73ade492022-12-27 20:54:41 +00001766 if (*p == '-' && generate_instr(cctx, ISN_NEGATENR) == NULL)
1767 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001768 }
1769 else if (numeric_only)
1770 {
1771 ++p;
1772 break;
1773 }
1774 else
1775 {
1776 int invert = *p == '!';
1777
1778 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1779 {
1780 if (p[-1] == '!')
1781 invert = !invert;
1782 --p;
1783 }
1784 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1785 return FAIL;
1786 }
1787 }
1788 *end = p;
1789 return OK;
1790}
1791
1792/*
1793 * Compile "(expression)": recursive!
1794 * Return FAIL/OK.
1795 */
1796 static int
1797compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1798{
1799 int ret;
1800 char_u *p = *arg + 1;
1801
1802 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1803 return FAIL;
1804 if (ppconst->pp_used <= PPSIZE - 10)
1805 {
1806 ret = compile_expr1(arg, cctx, ppconst);
1807 }
1808 else
1809 {
1810 // Not enough space in ppconst, flush constants.
1811 if (generate_ppconst(cctx, ppconst) == FAIL)
1812 return FAIL;
1813 ret = compile_expr0(arg, cctx);
1814 }
1815 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1816 return FAIL;
1817 if (**arg == ')')
1818 ++*arg;
1819 else if (ret == OK)
1820 {
1821 emsg(_(e_missing_closing_paren));
1822 ret = FAIL;
1823 }
1824 return ret;
1825}
1826
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001827static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001828
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001829/*
1830 * Compile whatever comes after "name" or "name()".
1831 * Advances "*arg" only when something was recognized.
1832 */
1833 static int
1834compile_subscript(
1835 char_u **arg,
1836 cctx_T *cctx,
1837 char_u *start_leader,
1838 char_u **end_leader,
1839 ppconst_T *ppconst)
1840{
1841 char_u *name_start = *end_leader;
1842 int keeping_dict = FALSE;
1843
1844 for (;;)
1845 {
1846 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001847 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001848
1849 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1850 {
1851 char_u *next = peek_next_line_from_context(cctx);
1852
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001853 // If a following line starts with "->{", "->(" or "->X" advance to
1854 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001855 // Also if a following line starts with ".x".
1856 if (next != NULL &&
1857 ((next[0] == '-' && next[1] == '>'
1858 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001859 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001860 || ASCII_ISALPHA(*skipwhite(next + 2))))
1861 || (next[0] == '.' && eval_isdictc(next[1]))))
1862 {
1863 next = next_line_from_context(cctx, TRUE);
1864 if (next == NULL)
1865 return FAIL;
1866 *arg = next;
1867 p = skipwhite(*arg);
1868 }
1869 }
1870
1871 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1872 // is not a function call.
1873 if (**arg == '(')
1874 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001875 int argcount = 0;
1876
1877 if (generate_ppconst(cctx, ppconst) == FAIL)
1878 return FAIL;
1879 ppconst->pp_is_const = FALSE;
1880
1881 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001882 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001883
1884 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001885 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001886 return FAIL;
1887 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1888 return FAIL;
1889 if (keeping_dict)
1890 {
1891 keeping_dict = FALSE;
1892 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1893 return FAIL;
1894 }
1895 }
1896 else if (*p == '-' && p[1] == '>')
1897 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001898 char_u *pstart = p;
1899 int alt;
1900 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001901
Bram Moolenaarc7349932022-01-16 20:59:39 +00001902 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001903 if (generate_ppconst(cctx, ppconst) == FAIL)
1904 return FAIL;
1905 ppconst->pp_is_const = FALSE;
1906
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001907 // Apply the '!', '-' and '+' first:
1908 // -1.0->func() works like (-1.0)->func()
1909 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1910 return FAIL;
1911
1912 p += 2;
1913 *arg = skipwhite(p);
1914 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001915
1916 // Three alternatives handled here:
1917 // 1. "base->name(" only a name, use compile_call()
1918 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1919 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001920 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001921 alt = 2;
1922 else
1923 {
1924 // alternative 1 or 3
1925 p = *arg;
1926 if (!eval_isnamec1(*p))
1927 {
1928 semsg(_(e_trailing_characters_str), pstart);
1929 return FAIL;
1930 }
1931 if (ASCII_ISALPHA(*p) && p[1] == ':')
1932 p += 2;
1933 for ( ; eval_isnamec(*p); ++p)
1934 ;
1935 if (*p == '(')
1936 {
1937 // alternative 1
1938 alt = 1;
1939 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1940 return FAIL;
1941 }
1942 else
1943 {
1944 // Must be alternative 3, find the "(". Only works within
1945 // one line.
1946 alt = 3;
1947 paren = vim_strchr(p, '(');
1948 if (paren == NULL)
1949 {
1950 semsg(_(e_missing_parenthesis_str), *arg);
1951 return FAIL;
1952 }
1953 }
1954 }
1955
1956 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001957 {
1958 int argcount = 1;
1959 garray_T *stack = &cctx->ctx_type_stack;
1960 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001961 int expr_isn_start = cctx->ctx_instr.ga_len;
1962 int expr_isn_end;
1963 int arg_isn_count;
1964
Bram Moolenaarc7349932022-01-16 20:59:39 +00001965 if (alt == 2)
1966 {
1967 // Funcref call: list->(Refs[2])(arg)
1968 // or lambda: list->((arg) => expr)(arg)
1969 //
1970 // Fist compile the function expression.
1971 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1972 return FAIL;
1973 }
1974 else
1975 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001976 int fail;
1977 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001978 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001979
Bram Moolenaarc7349932022-01-16 20:59:39 +00001980 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001981
1982 // instead of using LOADG for "import.Func" use PUSHFUNC
1983 ++paren_follows_after_expr;
1984
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001985 // do not look in the next line
1986 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001987
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001988 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001989 || *skipwhite(*arg) != NUL;
1990 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001991 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001992 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001993
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001994 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001995 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001996 if (did_emsg == prev_did_emsg)
1997 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001998 return FAIL;
1999 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002000 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002001
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002002 // Compile the arguments.
2003 if (**arg != '(')
2004 {
2005 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002006 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002007 else
2008 semsg(_(e_missing_parenthesis_str), *arg);
2009 return FAIL;
2010 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002011
2012 // Remember the next instruction index, where the instructions
2013 // for arguments are being written.
2014 expr_isn_end = cctx->ctx_instr.ga_len;
2015
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002016 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002017 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2018 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002019 return FAIL;
2020
2021 // Move the instructions for the arguments to before the
2022 // instructions of the expression and move the type of the
2023 // expression after the argument types. This is what ISN_PCALL
2024 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002025 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2026 if (arg_isn_count > 0)
2027 {
2028 int expr_isn_count = expr_isn_end - expr_isn_start;
2029 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002030 type_T *decl_type;
2031 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002032
2033 if (isn == NULL)
2034 return FAIL;
2035 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2036 + expr_isn_start,
2037 sizeof(isn_T) * expr_isn_count);
2038 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2039 + expr_isn_start,
2040 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2041 sizeof(isn_T) * arg_isn_count);
2042 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2043 + expr_isn_start + arg_isn_count,
2044 isn, sizeof(isn_T) * expr_isn_count);
2045 vim_free(isn);
2046
Bram Moolenaar078a4612022-01-04 15:17:03 +00002047 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2048 type = typep->type_curr;
2049 decl_type = typep->type_decl;
2050 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2051 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2052 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002053 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002054 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2055 typep->type_curr = type;
2056 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002057 }
2058
Bram Moolenaar078a4612022-01-04 15:17:03 +00002059 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002060 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2061 return FAIL;
2062 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002063
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002064 if (keeping_dict)
2065 {
2066 keeping_dict = FALSE;
2067 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2068 return FAIL;
2069 }
2070 }
2071 else if (**arg == '[')
2072 {
2073 int is_slice = FALSE;
2074
2075 // list index: list[123]
2076 // dict member: dict[key]
2077 // string index: text[123]
2078 // blob index: blob[123]
2079 if (generate_ppconst(cctx, ppconst) == FAIL)
2080 return FAIL;
2081 ppconst->pp_is_const = FALSE;
2082
2083 ++p;
2084 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2085 return FAIL;
2086 if (**arg == ':')
2087 {
2088 // missing first index is equal to zero
2089 generate_PUSHNR(cctx, 0);
2090 }
2091 else
2092 {
2093 if (compile_expr0(arg, cctx) == FAIL)
2094 return FAIL;
2095 if (**arg == ':')
2096 {
2097 semsg(_(e_white_space_required_before_and_after_str_at_str),
2098 ":", *arg);
2099 return FAIL;
2100 }
2101 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2102 return FAIL;
2103 *arg = skipwhite(*arg);
2104 }
2105 if (**arg == ':')
2106 {
2107 is_slice = TRUE;
2108 ++*arg;
2109 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2110 {
2111 semsg(_(e_white_space_required_before_and_after_str_at_str),
2112 ":", *arg);
2113 return FAIL;
2114 }
2115 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2116 return FAIL;
2117 if (**arg == ']')
2118 // missing second index is equal to end of string
2119 generate_PUSHNR(cctx, -1);
2120 else
2121 {
2122 if (compile_expr0(arg, cctx) == FAIL)
2123 return FAIL;
2124 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2125 return FAIL;
2126 *arg = skipwhite(*arg);
2127 }
2128 }
2129
2130 if (**arg != ']')
2131 {
2132 emsg(_(e_missing_closing_square_brace));
2133 return FAIL;
2134 }
2135 *arg = *arg + 1;
2136
2137 if (keeping_dict)
2138 {
2139 keeping_dict = FALSE;
2140 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2141 return FAIL;
2142 }
2143 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2144 return FAIL;
2145 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002146 else if (*p == '.'
2147 && (type = get_type_on_stack(cctx, 0)) != &t_unknown
2148 && (type->tt_type == VAR_CLASS || type->tt_type == VAR_OBJECT))
2149 {
2150 // class member: SomeClass.varname
2151 // class method: SomeClass.SomeMethod()
2152 // class constructor: SomeClass.new()
2153 // object member: someObject.varname, this.varname
2154 // object method: someObject.SomeMethod(), this.SomeMethod()
2155 if (compile_class_object_index(cctx, arg, type) == FAIL)
2156 return FAIL;
2157 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002158 else if (*p == '.' && p[1] != '.')
2159 {
2160 // dictionary member: dict.name
2161 if (generate_ppconst(cctx, ppconst) == FAIL)
2162 return FAIL;
2163 ppconst->pp_is_const = FALSE;
2164
2165 *arg = p + 1;
2166 if (IS_WHITE_OR_NUL(**arg))
2167 {
2168 emsg(_(e_missing_name_after_dot));
2169 return FAIL;
2170 }
2171 p = *arg;
2172 if (eval_isdictc(*p))
2173 while (eval_isnamec(*p))
2174 MB_PTR_ADV(p);
2175 if (p == *arg)
2176 {
2177 semsg(_(e_syntax_error_at_str), *arg);
2178 return FAIL;
2179 }
2180 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2181 return FAIL;
2182 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2183 return FAIL;
2184 keeping_dict = TRUE;
2185 *arg = p;
2186 }
2187 else
2188 break;
2189 }
2190
2191 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2192 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002193 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2194 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002195 return FAIL;
2196
2197 return OK;
2198}
2199
2200/*
2201 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2202 * "arg" is advanced until after the expression, skipping white space.
2203 *
2204 * If the value is a constant "ppconst->pp_used" will be non-zero.
2205 * Before instructions are generated, any values in "ppconst" will generated.
2206 *
2207 * This is the compiling equivalent of eval1(), eval2(), etc.
2208 */
2209
2210/*
2211 * number number constant
2212 * 0zFFFFFFFF Blob constant
2213 * "string" string constant
2214 * 'string' literal string constant
2215 * &option-name option value
2216 * @r register contents
2217 * identifier variable value
2218 * function() function call
2219 * $VAR environment variable
2220 * (expression) nested expression
2221 * [expr, expr] List
2222 * {key: val, [key]: val} Dictionary
2223 *
2224 * Also handle:
2225 * ! in front logical NOT
2226 * - in front unary minus
2227 * + in front unary plus (ignored)
2228 * trailing (arg) funcref/partial call
2229 * trailing [] subscript in String or List
2230 * trailing .name entry in Dictionary
2231 * trailing ->name() method call
2232 */
2233 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002234compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002235 char_u **arg,
2236 cctx_T *cctx,
2237 ppconst_T *ppconst)
2238{
2239 char_u *start_leader, *end_leader;
2240 int ret = OK;
2241 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2242 int used_before = ppconst->pp_used;
2243
2244 ppconst->pp_is_const = FALSE;
2245
2246 /*
2247 * Skip '!', '-' and '+' characters. They are handled later.
2248 */
2249 start_leader = *arg;
2250 if (eval_leader(arg, TRUE) == FAIL)
2251 return FAIL;
2252 end_leader = *arg;
2253
2254 rettv->v_type = VAR_UNKNOWN;
2255 switch (**arg)
2256 {
2257 /*
2258 * Number constant.
2259 */
2260 case '0': // also for blob starting with 0z
2261 case '1':
2262 case '2':
2263 case '3':
2264 case '4':
2265 case '5':
2266 case '6':
2267 case '7':
2268 case '8':
2269 case '9':
2270 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2271 return FAIL;
2272 // Apply "-" and "+" just before the number now, right to
2273 // left. Matters especially when "->" follows. Stops at
2274 // '!'.
2275 if (apply_leader(rettv, TRUE,
2276 start_leader, &end_leader) == FAIL)
2277 {
2278 clear_tv(rettv);
2279 return FAIL;
2280 }
2281 break;
2282
2283 /*
2284 * String constant: "string".
2285 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002286 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002287 return FAIL;
2288 break;
2289
2290 /*
2291 * Literal string constant: 'str''ing'.
2292 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002293 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002294 return FAIL;
2295 break;
2296
2297 /*
2298 * Constant Vim variable.
2299 */
2300 case 'v': get_vim_constant(arg, rettv);
2301 ret = NOTDONE;
2302 break;
2303
2304 /*
2305 * "true" constant
2306 */
2307 case 't': if (STRNCMP(*arg, "true", 4) == 0
2308 && !eval_isnamec((*arg)[4]))
2309 {
2310 *arg += 4;
2311 rettv->v_type = VAR_BOOL;
2312 rettv->vval.v_number = VVAL_TRUE;
2313 }
2314 else
2315 ret = NOTDONE;
2316 break;
2317
2318 /*
2319 * "false" constant
2320 */
2321 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2322 && !eval_isnamec((*arg)[5]))
2323 {
2324 *arg += 5;
2325 rettv->v_type = VAR_BOOL;
2326 rettv->vval.v_number = VVAL_FALSE;
2327 }
2328 else
2329 ret = NOTDONE;
2330 break;
2331
2332 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002333 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002334 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002335 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002336 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002337 char_u *p = *arg + 4;
2338 int len;
2339
2340 for (len = 0; eval_isnamec(p[len]); ++len)
2341 ;
2342 ret = handle_predefined(*arg, len + 4, rettv);
2343 if (ret == FAIL)
2344 ret = NOTDONE;
2345 else
2346 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002347 }
2348 else
2349 ret = NOTDONE;
2350 break;
2351
2352 /*
2353 * List: [expr, expr]
2354 */
2355 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2356 return FAIL;
2357 ret = compile_list(arg, cctx, ppconst);
2358 break;
2359
2360 /*
2361 * Dictionary: {'key': val, 'key': val}
2362 */
2363 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2364 return FAIL;
2365 ret = compile_dict(arg, cctx, ppconst);
2366 break;
2367
2368 /*
2369 * Option value: &name
2370 */
2371 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2372 return FAIL;
2373 ret = compile_get_option(arg, cctx);
2374 break;
2375
2376 /*
2377 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002378 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002379 */
2380 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2381 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002382 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2383 ret = compile_interp_string(arg, cctx);
2384 else
2385 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002386 break;
2387
2388 /*
2389 * Register contents: @r.
2390 */
2391 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2392 return FAIL;
2393 ret = compile_get_register(arg, cctx);
2394 break;
2395 /*
2396 * nested expression: (expression).
2397 * lambda: (arg, arg) => expr
2398 * funcref: (arg, arg) => { statement }
2399 */
2400 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2401 ret = compile_lambda(arg, cctx);
2402 if (ret == NOTDONE)
2403 ret = compile_parenthesis(arg, cctx, ppconst);
2404 break;
2405
2406 default: ret = NOTDONE;
2407 break;
2408 }
2409 if (ret == FAIL)
2410 return FAIL;
2411
2412 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2413 {
2414 if (cctx->ctx_skip == SKIP_YES)
2415 clear_tv(rettv);
2416 else
2417 // A constant expression can possibly be handled compile time,
2418 // return the value instead of generating code.
2419 ++ppconst->pp_used;
2420 }
2421 else if (ret == NOTDONE)
2422 {
2423 char_u *p;
2424 int r;
2425
2426 if (!eval_isnamec1(**arg))
2427 {
2428 if (!vim9_bad_comment(*arg))
2429 {
2430 if (ends_excmd(*skipwhite(*arg)))
2431 semsg(_(e_empty_expression_str), *arg);
2432 else
2433 semsg(_(e_name_expected_str), *arg);
2434 }
2435 return FAIL;
2436 }
2437
2438 // "name" or "name()"
2439 p = to_name_end(*arg, TRUE);
2440 if (p - *arg == (size_t)1 && **arg == '_')
2441 {
2442 emsg(_(e_cannot_use_underscore_here));
2443 return FAIL;
2444 }
2445
2446 if (*p == '(')
2447 {
2448 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2449 }
2450 else
2451 {
2452 if (cctx->ctx_skip != SKIP_YES
2453 && generate_ppconst(cctx, ppconst) == FAIL)
2454 return FAIL;
2455 r = compile_load(arg, p, cctx, TRUE, TRUE);
2456 }
2457 if (r == FAIL)
2458 return FAIL;
2459 }
2460
2461 // Handle following "[]", ".member", etc.
2462 // Then deal with prefixed '-', '+' and '!', if not done already.
2463 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2464 ppconst) == FAIL)
2465 return FAIL;
2466 if (ppconst->pp_used > 0)
2467 {
2468 // apply the '!', '-' and '+' before the constant
2469 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2470 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2471 return FAIL;
2472 return OK;
2473 }
2474 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2475 return FAIL;
2476 return OK;
2477}
2478
2479/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002480 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002481 */
2482 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002483compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002484{
2485 type_T *want_type = NULL;
2486
2487 // Recognize <type>
2488 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2489 {
2490 ++*arg;
2491 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2492 if (want_type == NULL)
2493 return FAIL;
2494
2495 if (**arg != '>')
2496 {
2497 if (*skipwhite(*arg) == '>')
2498 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2499 else
2500 emsg(_(e_missing_gt));
2501 return FAIL;
2502 }
2503 ++*arg;
2504 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2505 return FAIL;
2506 }
2507
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002508 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002509 return FAIL;
2510
2511 if (want_type != NULL)
2512 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002513 type_T *actual;
2514 where_T where = WHERE_INIT;
2515
2516 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002517 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002518 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002519 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002520 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2521 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002522 return FAIL;
2523 }
2524 }
2525
2526 return OK;
2527}
2528
2529/*
2530 * * number multiplication
2531 * / number division
2532 * % number modulo
2533 */
2534 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002535compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002536{
2537 char_u *op;
2538 char_u *next;
2539 int ppconst_used = ppconst->pp_used;
2540
2541 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002542 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002543 return FAIL;
2544
2545 /*
2546 * Repeat computing, until no "*", "/" or "%" is following.
2547 */
2548 for (;;)
2549 {
2550 op = may_peek_next_line(cctx, *arg, &next);
2551 if (*op != '*' && *op != '/' && *op != '%')
2552 break;
2553 if (next != NULL)
2554 {
2555 *arg = next_line_from_context(cctx, TRUE);
2556 op = skipwhite(*arg);
2557 }
2558
2559 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2560 {
2561 error_white_both(op, 1);
2562 return FAIL;
2563 }
2564 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2565 return FAIL;
2566
2567 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002568 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002569 return FAIL;
2570
2571 if (ppconst->pp_used == ppconst_used + 2
2572 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2573 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2574 {
2575 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2576 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2577 varnumber_T res = 0;
2578 int failed = FALSE;
2579
2580 // both are numbers: compute the result
2581 switch (*op)
2582 {
2583 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2584 break;
2585 case '/': res = num_divide(tv1->vval.v_number,
2586 tv2->vval.v_number, &failed);
2587 break;
2588 case '%': res = num_modulus(tv1->vval.v_number,
2589 tv2->vval.v_number, &failed);
2590 break;
2591 }
2592 if (failed)
2593 return FAIL;
2594 tv1->vval.v_number = res;
2595 --ppconst->pp_used;
2596 }
2597 else
2598 {
2599 generate_ppconst(cctx, ppconst);
2600 generate_two_op(cctx, op);
2601 }
2602 }
2603
2604 return OK;
2605}
2606
2607/*
2608 * + number addition or list/blobl concatenation
2609 * - number subtraction
2610 * .. string concatenation
2611 */
2612 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002613compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002614{
2615 char_u *op;
2616 char_u *next;
2617 int oplen;
2618 int ppconst_used = ppconst->pp_used;
2619
2620 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002621 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002622 return FAIL;
2623
2624 /*
2625 * Repeat computing, until no "+", "-" or ".." is following.
2626 */
2627 for (;;)
2628 {
2629 op = may_peek_next_line(cctx, *arg, &next);
2630 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2631 break;
2632 if (op[0] == op[1] && *op != '.' && next)
2633 // Finding "++" or "--" on the next line is a separate command.
2634 // But ".." is concatenation.
2635 break;
2636 oplen = (*op == '.' ? 2 : 1);
2637 if (next != NULL)
2638 {
2639 *arg = next_line_from_context(cctx, TRUE);
2640 op = skipwhite(*arg);
2641 }
2642
2643 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2644 {
2645 error_white_both(op, oplen);
2646 return FAIL;
2647 }
2648
2649 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2650 return FAIL;
2651
2652 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002653 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002654 return FAIL;
2655
2656 if (ppconst->pp_used == ppconst_used + 2
2657 && (*op == '.'
2658 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2659 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2660 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2661 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2662 {
2663 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2664 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2665
2666 // concat/subtract/add constant numbers
2667 if (*op == '+')
2668 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2669 else if (*op == '-')
2670 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2671 else
2672 {
2673 // concatenate constant strings
2674 char_u *s1 = tv1->vval.v_string;
2675 char_u *s2 = tv2->vval.v_string;
2676 size_t len1 = STRLEN(s1);
2677
2678 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2679 if (tv1->vval.v_string == NULL)
2680 {
2681 clear_ppconst(ppconst);
2682 return FAIL;
2683 }
2684 mch_memmove(tv1->vval.v_string, s1, len1);
2685 STRCPY(tv1->vval.v_string + len1, s2);
2686 vim_free(s1);
2687 vim_free(s2);
2688 }
2689 --ppconst->pp_used;
2690 }
2691 else
2692 {
2693 generate_ppconst(cctx, ppconst);
2694 ppconst->pp_is_const = FALSE;
2695 if (*op == '.')
2696 {
2697 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2698 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2699 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002700 if (generate_CONCAT(cctx, 2) == FAIL)
2701 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002702 }
2703 else
2704 generate_two_op(cctx, op);
2705 }
2706 }
2707
2708 return OK;
2709}
2710
2711/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002712 * expr6a >> expr6b
2713 * expr6a << expr6b
2714 *
2715 * Produces instructions:
2716 * OPNR bitwise left or right shift
2717 */
2718 static int
2719compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2720{
2721 exprtype_T type = EXPR_UNKNOWN;
2722 char_u *p;
2723 char_u *next;
2724 int len = 2;
2725 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002726 isn_T *isn;
2727
2728 // get the first variable
2729 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2730 return FAIL;
2731
2732 /*
2733 * Repeat computing, until no "+", "-" or ".." is following.
2734 */
2735 for (;;)
2736 {
2737 type = EXPR_UNKNOWN;
2738
2739 p = may_peek_next_line(cctx, *arg, &next);
2740 if (p[0] == '<' && p[1] == '<')
2741 type = EXPR_LSHIFT;
2742 else if (p[0] == '>' && p[1] == '>')
2743 type = EXPR_RSHIFT;
2744
2745 if (type == EXPR_UNKNOWN)
2746 return OK;
2747
2748 // Handle a bitwise left or right shift operator
2749 if (ppconst->pp_used == ppconst_used + 1)
2750 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002751 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002752 {
2753 // left operand should be a number
2754 emsg(_(e_bitshift_ops_must_be_number));
2755 return FAIL;
2756 }
2757 }
2758 else
2759 {
2760 type_T *t = get_type_on_stack(cctx, 0);
2761
2762 if (need_type(t, &t_number, 0, 0, cctx, FALSE, FALSE) == FAIL)
2763 {
2764 emsg(_(e_bitshift_ops_must_be_number));
2765 return FAIL;
2766 }
2767 }
2768
2769 if (next != NULL)
2770 {
2771 *arg = next_line_from_context(cctx, TRUE);
2772 p = skipwhite(*arg);
2773 }
2774
2775 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2776 {
2777 error_white_both(p, len);
2778 return FAIL;
2779 }
2780
2781 // get the second variable
2782 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2783 return FAIL;
2784
2785 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2786 return FAIL;
2787
2788 if (ppconst->pp_used == ppconst_used + 2)
2789 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002790 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2791 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2792
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002793 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002794 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2795 {
2796 // right operand should be a positive number
2797 if (tv2->v_type != VAR_NUMBER)
2798 emsg(_(e_bitshift_ops_must_be_number));
2799 else
dundargocc57b5bc2022-11-02 13:30:51 +00002800 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002801 return FAIL;
2802 }
2803
2804 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2805 tv1->vval.v_number = 0;
2806 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002807 tv1->vval.v_number =
2808 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002809 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002810 tv1->vval.v_number =
2811 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002812 clear_tv(tv2);
2813 --ppconst->pp_used;
2814 }
2815 else
2816 {
2817 if (need_type(get_type_on_stack(cctx, 0), &t_number, 0, 0, cctx,
2818 FALSE, FALSE) == FAIL)
2819 {
2820 emsg(_(e_bitshift_ops_must_be_number));
2821 return FAIL;
2822 }
2823
2824 generate_ppconst(cctx, ppconst);
2825
2826 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2827 if (isn == NULL)
2828 return FAIL;
2829
2830 if (isn != NULL)
2831 isn->isn_arg.op.op_type = type;
2832 }
2833 }
2834
2835 return OK;
2836}
2837
2838/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002839 * expr5a == expr5b
2840 * expr5a =~ expr5b
2841 * expr5a != expr5b
2842 * expr5a !~ expr5b
2843 * expr5a > expr5b
2844 * expr5a >= expr5b
2845 * expr5a < expr5b
2846 * expr5a <= expr5b
2847 * expr5a is expr5b
2848 * expr5a isnot expr5b
2849 *
2850 * Produces instructions:
2851 * EVAL expr5a Push result of "expr5a"
2852 * EVAL expr5b Push result of "expr5b"
2853 * COMPARE one of the compare instructions
2854 */
2855 static int
2856compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2857{
2858 exprtype_T type = EXPR_UNKNOWN;
2859 char_u *p;
2860 char_u *next;
2861 int len = 2;
2862 int type_is = FALSE;
2863 int ppconst_used = ppconst->pp_used;
2864
2865 // get the first variable
2866 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2867 return FAIL;
2868
2869 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002870
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002871 type = get_compare_type(p, &len, &type_is);
2872
2873 /*
2874 * If there is a comparative operator, use it.
2875 */
2876 if (type != EXPR_UNKNOWN)
2877 {
2878 int ic = FALSE; // Default: do not ignore case
2879
2880 if (next != NULL)
2881 {
2882 *arg = next_line_from_context(cctx, TRUE);
2883 p = skipwhite(*arg);
2884 }
2885 if (type_is && (p[len] == '?' || p[len] == '#'))
2886 {
2887 semsg(_(e_invalid_expression_str), *arg);
2888 return FAIL;
2889 }
2890 // extra question mark appended: ignore case
2891 if (p[len] == '?')
2892 {
2893 ic = TRUE;
2894 ++len;
2895 }
2896 // extra '#' appended: match case (ignored)
2897 else if (p[len] == '#')
2898 ++len;
2899 // nothing appended: match case
2900
2901 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2902 {
2903 error_white_both(p, len);
2904 return FAIL;
2905 }
2906
2907 // get the second variable
2908 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2909 return FAIL;
2910
2911 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2912 return FAIL;
2913
2914 if (ppconst->pp_used == ppconst_used + 2)
2915 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002916 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002917 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2918 int ret;
2919
2920 // Both sides are a constant, compute the result now.
2921 // First check for a valid combination of types, this is more
2922 // strict than typval_compare().
2923 if (check_compare_types(type, tv1, tv2) == FAIL)
2924 ret = FAIL;
2925 else
2926 {
2927 ret = typval_compare(tv1, tv2, type, ic);
2928 tv1->v_type = VAR_BOOL;
2929 tv1->vval.v_number = tv1->vval.v_number
2930 ? VVAL_TRUE : VVAL_FALSE;
2931 clear_tv(tv2);
2932 --ppconst->pp_used;
2933 }
2934 return ret;
2935 }
2936
2937 generate_ppconst(cctx, ppconst);
2938 return generate_COMPARE(cctx, type, ic);
2939 }
2940
2941 return OK;
2942}
2943
2944static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2945
2946/*
2947 * Compile || or &&.
2948 */
2949 static int
2950compile_and_or(
2951 char_u **arg,
2952 cctx_T *cctx,
2953 char *op,
2954 ppconst_T *ppconst,
2955 int ppconst_used UNUSED)
2956{
2957 char_u *next;
2958 char_u *p = may_peek_next_line(cctx, *arg, &next);
2959 int opchar = *op;
2960
2961 if (p[0] == opchar && p[1] == opchar)
2962 {
2963 garray_T *instr = &cctx->ctx_instr;
2964 garray_T end_ga;
2965 int save_skip = cctx->ctx_skip;
2966
2967 /*
2968 * Repeat until there is no following "||" or "&&"
2969 */
2970 ga_init2(&end_ga, sizeof(int), 10);
2971 while (p[0] == opchar && p[1] == opchar)
2972 {
2973 long start_lnum = SOURCING_LNUM;
2974 long save_sourcing_lnum;
2975 int start_ctx_lnum = cctx->ctx_lnum;
2976 int save_lnum;
2977 int const_used;
2978 int status;
2979 jumpwhen_T jump_when = opchar == '|'
2980 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2981
2982 if (next != NULL)
2983 {
2984 *arg = next_line_from_context(cctx, TRUE);
2985 p = skipwhite(*arg);
2986 }
2987
2988 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2989 {
2990 semsg(_(e_white_space_required_before_and_after_str_at_str),
2991 op, p);
2992 ga_clear(&end_ga);
2993 return FAIL;
2994 }
2995
2996 save_sourcing_lnum = SOURCING_LNUM;
2997 SOURCING_LNUM = start_lnum;
2998 save_lnum = cctx->ctx_lnum;
2999 cctx->ctx_lnum = start_ctx_lnum;
3000
3001 status = check_ppconst_bool(ppconst);
3002 if (status != FAIL)
3003 {
3004 // Use the last ppconst if possible.
3005 if (ppconst->pp_used > 0)
3006 {
3007 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3008 int is_true = tv2bool(tv);
3009
3010 if ((is_true && opchar == '|')
3011 || (!is_true && opchar == '&'))
3012 {
3013 // For "false && expr" and "true || expr" the "expr"
3014 // does not need to be evaluated.
3015 cctx->ctx_skip = SKIP_YES;
3016 clear_tv(tv);
3017 tv->v_type = VAR_BOOL;
3018 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3019 }
3020 else
3021 {
3022 // For "true && expr" and "false || expr" only "expr"
3023 // needs to be evaluated.
3024 --ppconst->pp_used;
3025 jump_when = JUMP_NEVER;
3026 }
3027 }
3028 else
3029 {
3030 // Every part must evaluate to a bool.
3031 status = bool_on_stack(cctx);
3032 }
3033 }
3034 if (status != FAIL)
3035 status = ga_grow(&end_ga, 1);
3036 cctx->ctx_lnum = save_lnum;
3037 if (status == FAIL)
3038 {
3039 ga_clear(&end_ga);
3040 return FAIL;
3041 }
3042
3043 if (jump_when != JUMP_NEVER)
3044 {
3045 if (cctx->ctx_skip != SKIP_YES)
3046 {
3047 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3048 ++end_ga.ga_len;
3049 }
3050 generate_JUMP(cctx, jump_when, 0);
3051 }
3052
3053 // eval the next expression
3054 SOURCING_LNUM = save_sourcing_lnum;
3055 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3056 {
3057 ga_clear(&end_ga);
3058 return FAIL;
3059 }
3060
3061 const_used = ppconst->pp_used;
3062 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3063 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3064 {
3065 ga_clear(&end_ga);
3066 return FAIL;
3067 }
3068
3069 // "0 || 1" results in true, "1 && 0" results in false.
3070 if (ppconst->pp_used == const_used + 1)
3071 {
3072 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3073
3074 if (tv->v_type == VAR_NUMBER
3075 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3076 {
3077 tv->vval.v_number = tv->vval.v_number == 1
3078 ? VVAL_TRUE : VVAL_FALSE;
3079 tv->v_type = VAR_BOOL;
3080 }
3081 }
3082
3083 p = may_peek_next_line(cctx, *arg, &next);
3084 }
3085
3086 if (check_ppconst_bool(ppconst) == FAIL)
3087 {
3088 ga_clear(&end_ga);
3089 return FAIL;
3090 }
3091
3092 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3093 // Every part must evaluate to a bool.
3094 if (bool_on_stack(cctx) == FAIL)
3095 {
3096 ga_clear(&end_ga);
3097 return FAIL;
3098 }
3099
3100 if (end_ga.ga_len > 0)
3101 {
3102 // Fill in the end label in all jumps.
3103 generate_ppconst(cctx, ppconst);
3104 while (end_ga.ga_len > 0)
3105 {
3106 isn_T *isn;
3107
3108 --end_ga.ga_len;
3109 isn = ((isn_T *)instr->ga_data)
3110 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3111 isn->isn_arg.jump.jump_where = instr->ga_len;
3112 }
3113 }
3114 ga_clear(&end_ga);
3115
3116 cctx->ctx_skip = save_skip;
3117 }
3118
3119 return OK;
3120}
3121
3122/*
3123 * expr4a && expr4a && expr4a logical AND
3124 *
3125 * Produces instructions:
3126 * EVAL expr4a Push result of "expr4a"
3127 * COND2BOOL convert to bool if needed
3128 * JUMP_IF_COND_FALSE end
3129 * EVAL expr4b Push result of "expr4b"
3130 * JUMP_IF_COND_FALSE end
3131 * EVAL expr4c Push result of "expr4c"
3132 * end:
3133 */
3134 static int
3135compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3136{
3137 int ppconst_used = ppconst->pp_used;
3138
3139 // get the first variable
3140 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3141 return FAIL;
3142
3143 // || and && work almost the same
3144 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3145}
3146
3147/*
3148 * expr3a || expr3b || expr3c logical OR
3149 *
3150 * Produces instructions:
3151 * EVAL expr3a Push result of "expr3a"
3152 * COND2BOOL convert to bool if needed
3153 * JUMP_IF_COND_TRUE end
3154 * EVAL expr3b Push result of "expr3b"
3155 * JUMP_IF_COND_TRUE end
3156 * EVAL expr3c Push result of "expr3c"
3157 * end:
3158 */
3159 static int
3160compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3161{
3162 int ppconst_used = ppconst->pp_used;
3163
3164 // eval the first expression
3165 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3166 return FAIL;
3167
3168 // || and && work almost the same
3169 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3170}
3171
3172/*
3173 * Toplevel expression: expr2 ? expr1a : expr1b
3174 * Produces instructions:
3175 * EVAL expr2 Push result of "expr2"
3176 * JUMP_IF_FALSE alt jump if false
3177 * EVAL expr1a
3178 * JUMP_ALWAYS end
3179 * alt: EVAL expr1b
3180 * end:
3181 *
3182 * Toplevel expression: expr2 ?? expr1
3183 * Produces instructions:
3184 * EVAL expr2 Push result of "expr2"
3185 * JUMP_AND_KEEP_IF_TRUE end jump if true
3186 * EVAL expr1
3187 * end:
3188 */
3189 int
3190compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3191{
3192 char_u *p;
3193 int ppconst_used = ppconst->pp_used;
3194 char_u *next;
3195
3196 // Ignore all kinds of errors when not producing code.
3197 if (cctx->ctx_skip == SKIP_YES)
3198 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003199 int prev_did_emsg = did_emsg;
3200
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003201 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003202 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003203 }
3204
3205 // Evaluate the first expression.
3206 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3207 return FAIL;
3208
3209 p = may_peek_next_line(cctx, *arg, &next);
3210 if (*p == '?')
3211 {
3212 int op_falsy = p[1] == '?';
3213 garray_T *instr = &cctx->ctx_instr;
3214 garray_T *stack = &cctx->ctx_type_stack;
3215 int alt_idx = instr->ga_len;
3216 int end_idx = 0;
3217 isn_T *isn;
3218 type_T *type1 = NULL;
3219 int has_const_expr = FALSE;
3220 int const_value = FALSE;
3221 int save_skip = cctx->ctx_skip;
3222
3223 if (next != NULL)
3224 {
3225 *arg = next_line_from_context(cctx, TRUE);
3226 p = skipwhite(*arg);
3227 }
3228
3229 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3230 {
3231 semsg(_(e_white_space_required_before_and_after_str_at_str),
3232 op_falsy ? "??" : "?", p);
3233 return FAIL;
3234 }
3235
3236 if (ppconst->pp_used == ppconst_used + 1)
3237 {
3238 // the condition is a constant, we know whether the ? or the :
3239 // expression is to be evaluated.
3240 has_const_expr = TRUE;
3241 if (op_falsy)
3242 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3243 else
3244 {
3245 int error = FALSE;
3246
3247 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3248 &error);
3249 if (error)
3250 return FAIL;
3251 }
3252 cctx->ctx_skip = save_skip == SKIP_YES ||
3253 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3254
3255 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3256 // "left ?? right" and "left" is truthy: produce "left"
3257 generate_ppconst(cctx, ppconst);
3258 else
3259 {
3260 clear_tv(&ppconst->pp_tv[ppconst_used]);
3261 --ppconst->pp_used;
3262 }
3263 }
3264 else
3265 {
3266 generate_ppconst(cctx, ppconst);
3267 if (op_falsy)
3268 end_idx = instr->ga_len;
3269 generate_JUMP(cctx, op_falsy
3270 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3271 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003272 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003273 }
3274
3275 // evaluate the second expression; any type is accepted
3276 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3277 return FAIL;
3278 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3279 return FAIL;
3280
3281 if (!has_const_expr)
3282 {
3283 generate_ppconst(cctx, ppconst);
3284
3285 if (!op_falsy)
3286 {
3287 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003288 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003289 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003290
3291 end_idx = instr->ga_len;
3292 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3293
3294 // jump here from JUMP_IF_FALSE
3295 isn = ((isn_T *)instr->ga_data) + alt_idx;
3296 isn->isn_arg.jump.jump_where = instr->ga_len;
3297 }
3298 }
3299
3300 if (!op_falsy)
3301 {
3302 // Check for the ":".
3303 p = may_peek_next_line(cctx, *arg, &next);
3304 if (*p != ':')
3305 {
3306 emsg(_(e_missing_colon_after_questionmark));
3307 return FAIL;
3308 }
3309 if (next != NULL)
3310 {
3311 *arg = next_line_from_context(cctx, TRUE);
3312 p = skipwhite(*arg);
3313 }
3314
3315 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3316 {
3317 semsg(_(e_white_space_required_before_and_after_str_at_str),
3318 ":", p);
3319 return FAIL;
3320 }
3321
3322 // evaluate the third expression
3323 if (has_const_expr)
3324 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3325 ? SKIP_YES : SKIP_NOT;
3326 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3327 return FAIL;
3328 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3329 return FAIL;
3330 }
3331
3332 if (!has_const_expr)
3333 {
3334 type_T **typep;
3335
3336 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003337 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003338
3339 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003340 typep = &((((type2_T *)stack->ga_data)
3341 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003342 common_type(type1, *typep, typep, cctx->ctx_type_list);
3343
3344 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3345 isn = ((isn_T *)instr->ga_data) + end_idx;
3346 isn->isn_arg.jump.jump_where = instr->ga_len;
3347 }
3348
3349 cctx->ctx_skip = save_skip;
3350 }
3351 return OK;
3352}
3353
3354/*
3355 * Toplevel expression.
3356 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3357 * Returns OK or FAIL.
3358 */
3359 int
3360compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3361{
3362 ppconst_T ppconst;
3363
3364 CLEAR_FIELD(ppconst);
3365 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3366 {
3367 clear_ppconst(&ppconst);
3368 return FAIL;
3369 }
3370 if (is_const != NULL)
3371 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3372 if (generate_ppconst(cctx, &ppconst) == FAIL)
3373 return FAIL;
3374 return OK;
3375}
3376
3377/*
3378 * Toplevel expression.
3379 */
3380 int
3381compile_expr0(char_u **arg, cctx_T *cctx)
3382{
3383 return compile_expr0_ext(arg, cctx, NULL);
3384}
3385
3386
3387#endif // defined(FEAT_EVAL)