blob: ca2d81ce640e914ce659177100c998336f044efc [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 */
256
257 static int
258compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
259{
260 if (VIM_ISWHITE((*arg)[1]))
261 {
262 semsg(_(e_no_white_space_allowed_after_str_str), ".", *arg);
263 return FAIL;
264 }
265
266 ++*arg;
267 char_u *name = *arg;
268 char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
269 if (name_end == name)
270 return FAIL;
271 size_t len = name_end - name;
272
273 class_T *cl = (class_T *)type->tt_member;
274 if (*name_end == '(')
275 {
276 // TODO
277 }
278 else if (type->tt_type == VAR_OBJECT)
279 {
280 for (int i = 0; i < cl->class_obj_member_count; ++i)
281 {
282 objmember_T *m = &cl->class_obj_members[i];
283 if (STRNCMP(name, m->om_name, len) == 0 && m->om_name[len] == NUL)
284 {
285 generate_OBJ_MEMBER(cctx, i, m->om_type);
286
287 *arg = name_end;
288 return OK;
289 }
290 }
291
292 semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
293 }
294 else
295 {
296 // TODO: class member
297 emsg("compile_class_object_index(): not handled");
298 }
299
300 return FAIL;
301}
302
303/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000304 * Generate an instruction to load script-local variable "name", without the
305 * leading "s:".
306 * Also finds imported variables.
307 */
308 int
309compile_load_scriptvar(
310 cctx_T *cctx,
311 char_u *name, // variable NUL terminated
312 char_u *start, // start of variable
Bram Moolenaard0132f42022-05-12 11:05:40 +0100313 char_u **end) // end of variable, may be NULL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000314{
315 scriptitem_T *si;
316 int idx;
317 imported_T *import;
318
319 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
320 return FAIL;
321 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000322 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000323 if (idx >= 0)
324 {
325 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
326
327 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
328 current_sctx.sc_sid, idx, sv->sv_type);
329 return OK;
330 }
331
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000332 import = end == NULL ? NULL : find_imported(name, 0, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000333 if (import != NULL)
334 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000335 char_u *p = skipwhite(*end);
336 char_u *exp_name;
337 int cc;
Bram Moolenaarffe6e642022-04-01 13:23:47 +0100338 ufunc_T *ufunc = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000339 type_T *type;
Bram Moolenaard041f422022-01-12 19:54:00 +0000340 int done = FALSE;
341 int res = OK;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000342
343 // Need to lookup the member.
344 if (*p != '.')
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000345 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000346 semsg(_(e_expected_dot_after_name_str), start);
347 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000348 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000349 ++p;
350 if (VIM_ISWHITE(*p))
351 {
352 emsg(_(e_no_white_space_allowed_after_dot));
353 return FAIL;
354 }
355
356 // isolate one name
357 exp_name = p;
358 while (eval_isnamec(*p))
359 ++p;
360 cc = *p;
361 *p = NUL;
362
Bram Moolenaard041f422022-01-12 19:54:00 +0000363 si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100364 if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
365 // "import autoload './dir/script.vim'" or
366 // "import autoload './autoload/script.vim'" - load script first
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100367 res = generate_SOURCE(cctx, import->imp_sid);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100368
369 if (res == OK)
370 {
371 if (si->sn_autoload_prefix != NULL
372 && si->sn_state == SN_STATE_NOT_LOADED)
373 {
374 char_u *auto_name =
375 concat_str(si->sn_autoload_prefix, exp_name);
376
377 // autoload script must be loaded later, access by the autoload
378 // name. If a '(' follows it must be a function. Otherwise we
379 // don't know, it can be "script.Func".
380 if (cc == '(' || paren_follows_after_expr)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100381 res = generate_PUSHFUNC(cctx, auto_name, &t_func_any, TRUE);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100382 else
383 res = generate_AUTOLOAD(cctx, auto_name, &t_any);
384 vim_free(auto_name);
385 done = TRUE;
386 }
387 else if (si->sn_import_autoload
388 && si->sn_state == SN_STATE_NOT_LOADED)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100389 {
390 // If a '(' follows it must be a function. Otherwise we don't
391 // know, it can be "script.Func".
392 if (cc == '(' || paren_follows_after_expr)
393 {
394 char_u sid_name[MAX_FUNC_NAME_LEN];
395
396 func_name_with_sid(exp_name, import->imp_sid, sid_name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100397 res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100398 }
399 else
400 res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
401 import->imp_sid, &t_any);
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100402 done = TRUE;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100403 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100404 else
405 {
406 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
407 cctx, NULL, TRUE);
408 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +0100409 }
Bram Moolenaarccbfd482022-03-31 16:18:23 +0100410
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000411 *p = cc;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000412 *end = p;
Bram Moolenaard041f422022-01-12 19:54:00 +0000413 if (done)
414 return res;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000415
416 if (idx < 0)
417 {
418 if (ufunc != NULL)
419 {
420 // function call or function reference
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100421 generate_PUSHFUNC(cctx, ufunc->uf_name, NULL, TRUE);
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000422 return OK;
423 }
424 return FAIL;
425 }
426
427 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
428 import->imp_sid,
429 idx,
430 type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000431 return OK;
432 }
433
Bram Moolenaard0132f42022-05-12 11:05:40 +0100434 // Can only get here if we know "name" is a script variable and not in a
435 // Vim9 script (variable is not in sn_var_vals): old style script.
436 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
Bram Moolenaar62aec932022-01-29 21:45:34 +0000437 &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000438}
439
440 static int
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000441generate_funcref(cctx_T *cctx, char_u *name, int has_g_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000442{
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000443 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar139575d2022-03-15 19:29:30 +0000444 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000445
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000446 // Reject a global non-autoload function found without the "g:" prefix.
447 if (ufunc == NULL || (!has_g_prefix && func_requires_g_prefix(ufunc)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000448 return FAIL;
449
450 // Need to compile any default values to get the argument types.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000451 compile_type = get_compile_type(ufunc);
452 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaar21dc8f12022-03-16 17:54:17 +0000453 && compile_def_function(ufunc, TRUE, compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000454 return FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100455 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000456}
457
458/*
459 * Compile a variable name into a load instruction.
460 * "end" points to just after the name.
461 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
462 * When "error" is FALSE do not give an error when not found.
463 */
464 int
465compile_load(
466 char_u **arg,
467 char_u *end_arg,
468 cctx_T *cctx,
469 int is_expr,
470 int error)
471{
472 type_T *type;
473 char_u *name = NULL;
474 char_u *end = end_arg;
475 int res = FAIL;
476 int prev_called_emsg = called_emsg;
477
478 if (*(*arg + 1) == ':')
479 {
480 if (end <= *arg + 2)
481 {
482 isntype_T isn_type;
483
484 // load dictionary of namespace
485 switch (**arg)
486 {
487 case 'g': isn_type = ISN_LOADGDICT; break;
488 case 'w': isn_type = ISN_LOADWDICT; break;
489 case 't': isn_type = ISN_LOADTDICT; break;
490 case 'b': isn_type = ISN_LOADBDICT; break;
491 default:
492 semsg(_(e_namespace_not_supported_str), *arg);
493 goto theend;
494 }
495 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
496 goto theend;
497 res = OK;
498 }
499 else
500 {
501 isntype_T isn_type = ISN_DROP;
502
503 // load namespaced variable
504 name = vim_strnsave(*arg + 2, end - (*arg + 2));
505 if (name == NULL)
506 return FAIL;
507
508 switch (**arg)
509 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100510 case 'v': res = generate_LOADV(cctx, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000511 break;
Bram Moolenaarafa048f2022-02-22 20:43:36 +0000512 case 's': if (current_script_is_vim9())
513 {
514 semsg(_(e_cannot_use_s_colon_in_vim9_script_str),
515 *arg);
516 vim_free(name);
517 return FAIL;
518 }
Kota Kato948a3892022-08-16 16:09:59 +0100519 if (is_expr && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000520 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000521 else
522 res = compile_load_scriptvar(cctx, name,
Bram Moolenaard0132f42022-05-12 11:05:40 +0100523 NULL, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000524 break;
525 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
526 {
527 if (is_expr && ASCII_ISUPPER(*name)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000528 && find_func(name, FALSE) != NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000529 res = generate_funcref(cctx, name, TRUE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000530 else
531 isn_type = ISN_LOADG;
532 }
533 else
534 {
535 isn_type = ISN_LOADAUTO;
536 vim_free(name);
537 name = vim_strnsave(*arg, end - *arg);
538 if (name == NULL)
539 return FAIL;
540 }
541 break;
542 case 'w': isn_type = ISN_LOADW; break;
543 case 't': isn_type = ISN_LOADT; break;
544 case 'b': isn_type = ISN_LOADB; break;
545 default: // cannot happen, just in case
546 semsg(_(e_namespace_not_supported_str), *arg);
547 goto theend;
548 }
549 if (isn_type != ISN_DROP)
550 {
551 // Global, Buffer-local, Window-local and Tabpage-local
552 // variables can be defined later, thus we don't check if it
553 // exists, give an error at runtime.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000554 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000555 }
556 }
557 }
558 else
559 {
560 size_t len = end - *arg;
561 int idx;
562 int gen_load = FALSE;
563 int gen_load_outer = 0;
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100564 int outer_loop_depth = -1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100565 int outer_loop_idx = -1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000566
567 name = vim_strnsave(*arg, end - *arg);
568 if (name == NULL)
569 return FAIL;
570
571 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
572 {
573 script_autoload(name, FALSE);
574 res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
575 }
576 else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
577 == OK)
578 {
579 if (gen_load_outer == 0)
580 gen_load = TRUE;
581 }
582 else
583 {
584 lvar_T lvar;
585
586 if (lookup_local(*arg, len, &lvar, cctx) == OK)
587 {
588 type = lvar.lv_type;
589 idx = lvar.lv_idx;
590 if (lvar.lv_from_outer != 0)
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100591 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000592 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100593 outer_loop_depth = lvar.lv_loop_depth;
594 outer_loop_idx = lvar.lv_loop_idx;
595 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000596 else
597 gen_load = TRUE;
598 }
599 else
600 {
601 // "var" can be script-local even without using "s:" if it
602 // already exists in a Vim9 script or when it's imported.
Bram Moolenaardce24412022-02-08 20:35:30 +0000603 if (script_var_exists(*arg, len, cctx, NULL) == OK
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000604 || find_imported(name, 0, FALSE) != NULL)
Bram Moolenaard0132f42022-05-12 11:05:40 +0100605 res = compile_load_scriptvar(cctx, name, *arg, &end);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000606
607 // When evaluating an expression and the name starts with an
608 // uppercase letter it can be a user defined function.
609 // generate_funcref() will fail if the function can't be found.
610 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000611 res = generate_funcref(cctx, name, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000612 }
613 }
614 if (gen_load)
615 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
616 if (gen_load_outer > 0)
617 {
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +0100618 res = generate_LOADOUTER(cctx, idx, gen_load_outer,
619 outer_loop_depth, outer_loop_idx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000620 cctx->ctx_outer_used = TRUE;
621 }
622 }
623
624 *arg = end;
625
626theend:
627 if (res == FAIL && error && called_emsg == prev_called_emsg)
628 semsg(_(e_variable_not_found_str), name);
629 vim_free(name);
630 return res;
631}
632
633/*
634 * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
LemonBoyf3b48952022-05-05 13:53:03 +0100635 * "str_offset" is the number of leading bytes to skip from the string.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000636 * Returns FAIL if compilation fails.
637 */
638 static int
LemonBoyf3b48952022-05-05 13:53:03 +0100639compile_string(isn_T *isn, cctx_T *cctx, int str_offset)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000640{
LemonBoyf3b48952022-05-05 13:53:03 +0100641 char_u *s = isn->isn_arg.string + str_offset;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000642 garray_T save_ga = cctx->ctx_instr;
643 int expr_res;
644 int trailing_error;
645 int instr_count;
646 isn_T *instr = NULL;
647
648 // Remove the string type from the stack.
649 --cctx->ctx_type_stack.ga_len;
650
651 // Temporarily reset the list of instructions so that the jump labels are
652 // correct.
653 cctx->ctx_instr.ga_len = 0;
654 cctx->ctx_instr.ga_maxlen = 0;
655 cctx->ctx_instr.ga_data = NULL;
656 expr_res = compile_expr0(&s, cctx);
657 s = skipwhite(s);
658 trailing_error = *s != NUL;
659
660 if (expr_res == FAIL || trailing_error
661 || GA_GROW_FAILS(&cctx->ctx_instr, 1))
662 {
663 if (trailing_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +0000664 semsg(_(e_trailing_characters_str), s);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000665 clear_instr_ga(&cctx->ctx_instr);
666 cctx->ctx_instr = save_ga;
667 ++cctx->ctx_type_stack.ga_len;
668 return FAIL;
669 }
670
671 // Move the generated instructions into the ISN_INSTR instruction, then
672 // restore the list of instructions.
673 instr_count = cctx->ctx_instr.ga_len;
674 instr = cctx->ctx_instr.ga_data;
675 instr[instr_count].isn_type = ISN_FINISH;
676
677 cctx->ctx_instr = save_ga;
678 vim_free(isn->isn_arg.string);
679 isn->isn_type = ISN_INSTR;
680 isn->isn_arg.instr = instr;
681 return OK;
682}
683
684/*
685 * Compile the argument expressions.
686 * "arg" points to just after the "(" and is advanced to after the ")"
687 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100688 int
LemonBoyf3b48952022-05-05 13:53:03 +0100689compile_arguments(
690 char_u **arg,
691 cctx_T *cctx,
692 int *argcount,
693 ca_special_T special_fn)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000694{
695 char_u *p = *arg;
696 char_u *whitep = *arg;
697 int must_end = FALSE;
698 int instr_count;
699
700 for (;;)
701 {
702 if (may_get_next_line(whitep, &p, cctx) == FAIL)
703 goto failret;
704 if (*p == ')')
705 {
706 *arg = p + 1;
707 return OK;
708 }
709 if (must_end)
710 {
711 semsg(_(e_missing_comma_before_argument_str), p);
712 return FAIL;
713 }
714
715 instr_count = cctx->ctx_instr.ga_len;
716 if (compile_expr0(&p, cctx) == FAIL)
717 return FAIL;
718 ++*argcount;
719
LemonBoyf3b48952022-05-05 13:53:03 +0100720 if (special_fn == CA_SEARCHPAIR && *argcount == 5
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000721 && cctx->ctx_instr.ga_len == instr_count + 1)
722 {
723 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
724
725 // {skip} argument of searchpair() can be compiled if not empty
726 if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
LemonBoyf3b48952022-05-05 13:53:03 +0100727 compile_string(isn, cctx, 0);
728 }
729 else if (special_fn == CA_SUBSTITUTE && *argcount == 3
730 && cctx->ctx_instr.ga_len == instr_count + 1)
731 {
732 isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
733
734 // {sub} argument of substitute() can be compiled if it starts
735 // with \=
736 if (isn->isn_type == ISN_PUSHS && isn->isn_arg.string[0] == '\\'
Bram Moolenaar4913d422022-10-17 13:13:32 +0100737 && isn->isn_arg.string[1] == '=')
LemonBoyf3b48952022-05-05 13:53:03 +0100738 compile_string(isn, cctx, 2);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000739 }
740
741 if (*p != ',' && *skipwhite(p) == ',')
742 {
743 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
744 p = skipwhite(p);
745 }
746 if (*p == ',')
747 {
748 ++p;
749 if (*p != NUL && !VIM_ISWHITE(*p))
750 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
751 }
752 else
753 must_end = TRUE;
754 whitep = p;
755 p = skipwhite(p);
756 }
757failret:
758 emsg(_(e_missing_closing_paren));
759 return FAIL;
760}
761
762/*
763 * Compile a function call: name(arg1, arg2)
764 * "arg" points to "name", "arg + varlen" to the "(".
765 * "argcount_init" is 1 for "value->method()"
766 * Instructions:
767 * EVAL arg1
768 * EVAL arg2
769 * BCALL / DCALL / UCALL
770 */
771 static int
772compile_call(
773 char_u **arg,
774 size_t varlen,
775 cctx_T *cctx,
776 ppconst_T *ppconst,
777 int argcount_init)
778{
779 char_u *name = *arg;
780 char_u *p;
781 int argcount = argcount_init;
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100782 char_u namebuf[MAX_FUNC_NAME_LEN];
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000783 char_u fname_buf[FLEN_FIXED + 1];
784 char_u *tofree = NULL;
785 int error = FCERR_NONE;
786 ufunc_T *ufunc = NULL;
787 int res = FAIL;
788 int is_autoload;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000789 int has_g_namespace;
LemonBoyf3b48952022-05-05 13:53:03 +0100790 ca_special_T special_fn;
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000791 imported_T *import;
792
793 if (varlen >= sizeof(namebuf))
794 {
795 semsg(_(e_name_too_long_str), name);
796 return FAIL;
797 }
798 vim_strncpy(namebuf, *arg, varlen);
799
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000800 import = find_imported(name, varlen, FALSE);
Bram Moolenaarf67c7172022-01-19 17:23:05 +0000801 if (import != NULL)
802 {
803 semsg(_(e_cannot_use_str_itself_it_is_imported), namebuf);
804 return FAIL;
805 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000806
807 // We can evaluate "has('name')" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100808 // We can evaluate "len('string')" at compile time.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000809 // We always evaluate "exists_compiled()" at compile time.
LemonBoy58f331a2022-04-02 21:59:06 +0100810 if ((varlen == 3
811 && (STRNCMP(*arg, "has", 3) == 0 || STRNCMP(*arg, "len", 3) == 0))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000812 || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
813 {
814 char_u *s = skipwhite(*arg + varlen + 1);
815 typval_T argvars[2];
816 int is_has = **arg == 'h';
LemonBoy58f331a2022-04-02 21:59:06 +0100817 int is_len = **arg == 'l';
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000818
819 argvars[0].v_type = VAR_UNKNOWN;
820 if (*s == '"')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100821 (void)eval_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000822 else if (*s == '\'')
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100823 (void)eval_lit_string(&s, &argvars[0], TRUE, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000824 s = skipwhite(s);
825 if (*s == ')' && argvars[0].v_type == VAR_STRING
826 && ((is_has && !dynamic_feature(argvars[0].vval.v_string))
827 || !is_has))
828 {
829 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
830
831 *arg = s + 1;
832 argvars[1].v_type = VAR_UNKNOWN;
833 tv->v_type = VAR_NUMBER;
834 tv->vval.v_number = 0;
835 if (is_has)
836 f_has(argvars, tv);
LemonBoy58f331a2022-04-02 21:59:06 +0100837 else if (is_len)
838 f_len(argvars, tv);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000839 else
840 f_exists(argvars, tv);
841 clear_tv(&argvars[0]);
842 ++ppconst->pp_used;
843 return OK;
844 }
845 clear_tv(&argvars[0]);
LemonBoy58f331a2022-04-02 21:59:06 +0100846 if (!is_has && !is_len)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000847 {
848 emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
849 return FAIL;
850 }
851 }
852
853 if (generate_ppconst(cctx, ppconst) == FAIL)
854 return FAIL;
855
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000856 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
857
858 // We handle the "skip" argument of searchpair() and searchpairpos()
859 // differently.
LemonBoyf3b48952022-05-05 13:53:03 +0100860 if ((varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
861 || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
862 || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
863 || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0))
864 special_fn = CA_SEARCHPAIR;
865 else if (varlen == 10 && STRNCMP(*arg, "substitute", 10) == 0)
866 special_fn = CA_SUBSTITUTE;
867 else
868 special_fn = CA_NOT_SPECIAL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000869
870 *arg = skipwhite(*arg + varlen + 1);
LemonBoyf3b48952022-05-05 13:53:03 +0100871 if (compile_arguments(arg, cctx, &argcount, special_fn) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000872 goto theend;
873
874 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
875 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
876 {
877 int idx;
878
879 // builtin function
880 idx = find_internal_func(name);
881 if (idx >= 0)
882 {
883 if (STRCMP(name, "flatten") == 0)
884 {
885 emsg(_(e_cannot_use_flatten_in_vim9_script));
886 goto theend;
887 }
888
889 if (STRCMP(name, "add") == 0 && argcount == 2)
890 {
Bram Moolenaareb4a9ba2022-02-01 12:47:07 +0000891 type_T *type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000892
893 // add() can be compiled to instructions if we know the type
894 if (type->tt_type == VAR_LIST)
895 {
896 // inline "add(list, item)" so that the type can be checked
897 res = generate_LISTAPPEND(cctx);
898 idx = -1;
899 }
900 else if (type->tt_type == VAR_BLOB)
901 {
902 // inline "add(blob, nr)" so that the type can be checked
903 res = generate_BLOBAPPEND(cctx);
904 idx = -1;
905 }
906 }
907
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100908 if ((STRCMP(name, "writefile") == 0 && argcount > 2)
909 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
Bram Moolenaar806a2732022-09-04 15:40:36 +0100910 {
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100911 // May have the "D" or "R" flag, reserve a variable for a
912 // deferred function call.
Bram Moolenaar806a2732022-09-04 15:40:36 +0100913 if (get_defer_var_idx(cctx) == 0)
914 idx = -1;
915 }
916
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000917 if (idx >= 0)
918 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
919 }
920 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100921 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000922 goto theend;
923 }
924
Bram Moolenaar62aec932022-01-29 21:45:34 +0000925 has_g_namespace = STRNCMP(namebuf, "g:", 2) == 0;
926
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000927 // An argument or local variable can be a function reference, this
928 // overrules a function name.
929 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
930 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
931 {
932 // If we can find the function by name generate the right call.
933 // Skip global functions here, a local funcref takes precedence.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +0000934 ufunc = find_func(name, FALSE);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000935 if (ufunc != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000936 {
Bram Moolenaar62aec932022-01-29 21:45:34 +0000937 if (!func_is_global(ufunc))
938 {
939 res = generate_CALL(cctx, ufunc, argcount);
940 goto theend;
941 }
942 if (!has_g_namespace
943 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL)
944 {
945 // A function name without g: prefix must be found locally.
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100946 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaar62aec932022-01-29 21:45:34 +0000947 goto theend;
948 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000949 }
950 }
951
952 // If the name is a variable, load it and use PCALL.
953 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaar848fadd2022-01-30 15:28:30 +0000954 // Not for some#Func(), it will be loaded later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000955 p = namebuf;
Bram Moolenaar62aec932022-01-29 21:45:34 +0000956 if (!has_g_namespace && !is_autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000957 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
958 {
Bram Moolenaar078a4612022-01-04 15:17:03 +0000959 type_T *type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000960
961 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
962 goto theend;
963 }
964
965 // If we can find a global function by name generate the right call.
966 if (ufunc != NULL)
967 {
968 res = generate_CALL(cctx, ufunc, argcount);
969 goto theend;
970 }
971
972 // A global function may be defined only later. Need to figure out at
973 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaar62aec932022-01-29 21:45:34 +0000974 if (has_g_namespace || is_autoload)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000975 res = generate_UCALL(cctx, name, argcount);
976 else
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100977 emsg_funcname(e_unknown_function_str, namebuf);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000978
979theend:
980 vim_free(tofree);
981 return res;
982}
983
984// like NAMESPACE_CHAR but with 'a' and 'l'.
985#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
986
987/*
988 * Find the end of a variable or function name. Unlike find_name_end() this
989 * does not recognize magic braces.
990 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
991 * Return a pointer to just after the name. Equal to "arg" if there is no
992 * valid name.
993 */
994 char_u *
995to_name_end(char_u *arg, int use_namespace)
996{
997 char_u *p;
998
999 // Quick check for valid starting character.
1000 if (!eval_isnamec1(*arg))
1001 return arg;
1002
1003 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
1004 // Include a namespace such as "s:var" and "v:var". But "n:" is not
1005 // and can be used in slice "[n:]".
1006 if (*p == ':' && (p != arg + 1
1007 || !use_namespace
1008 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
1009 break;
1010 return p;
1011}
1012
1013/*
1014 * Like to_name_end() but also skip over a list or dict constant.
1015 * Also accept "<SNR>123_Func".
1016 * This intentionally does not handle line continuation.
1017 */
1018 char_u *
1019to_name_const_end(char_u *arg)
1020{
1021 char_u *p = arg;
1022 typval_T rettv;
1023
1024 if (STRNCMP(p, "<SNR>", 5) == 0)
1025 p = skipdigits(p + 5);
1026 p = to_name_end(p, TRUE);
1027 if (p == arg && *arg == '[')
1028 {
1029
1030 // Can be "[1, 2, 3]->Func()".
1031 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
1032 p = arg;
1033 }
1034 return p;
1035}
1036
1037/*
1038 * parse a list: [expr, expr]
1039 * "*arg" points to the '['.
1040 * ppconst->pp_is_const is set if all items are a constant.
1041 */
1042 static int
1043compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1044{
1045 char_u *p = skipwhite(*arg + 1);
1046 char_u *whitep = *arg + 1;
1047 int count = 0;
1048 int is_const;
1049 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001050 int must_end = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001051
1052 for (;;)
1053 {
1054 if (may_get_next_line(whitep, &p, cctx) == FAIL)
1055 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001056 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001057 return FAIL;
1058 }
1059 if (*p == ',')
1060 {
1061 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
1062 return FAIL;
1063 }
1064 if (*p == ']')
1065 {
1066 ++p;
1067 break;
1068 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001069 if (must_end)
1070 {
1071 semsg(_(e_missing_comma_in_list_str), p);
1072 return FAIL;
1073 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001074 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
1075 return FAIL;
1076 if (!is_const)
1077 is_all_const = FALSE;
1078 ++count;
1079 if (*p == ',')
1080 {
1081 ++p;
1082 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
1083 {
1084 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
1085 return FAIL;
1086 }
1087 }
Bram Moolenaar2984ed32022-08-20 14:51:17 +01001088 else
1089 must_end = TRUE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001090 whitep = p;
1091 p = skipwhite(p);
1092 }
1093 *arg = p;
1094
1095 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001096 return generate_NEWLIST(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001097}
1098
1099/*
1100 * Parse a lambda: "(arg, arg) => expr"
1101 * "*arg" points to the '('.
1102 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
1103 */
1104 static int
1105compile_lambda(char_u **arg, cctx_T *cctx)
1106{
1107 int r;
1108 typval_T rettv;
1109 ufunc_T *ufunc;
1110 evalarg_T evalarg;
1111
1112 init_evalarg(&evalarg);
1113 evalarg.eval_flags = EVAL_EVALUATE;
1114 evalarg.eval_cctx = cctx;
1115
1116 // Get the funcref in "rettv".
1117 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
1118 if (r != OK)
1119 {
1120 clear_evalarg(&evalarg, NULL);
1121 return r;
1122 }
1123
1124 // "rettv" will now be a partial referencing the function.
1125 ufunc = rettv.vval.v_partial->pt_func;
1126 ++ufunc->uf_refcount;
1127 clear_tv(&rettv);
1128
1129 // Compile it here to get the return type. The return type is optional,
1130 // when it's missing use t_unknown. This is recognized in
1131 // compile_return().
1132 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
1133 ufunc->uf_ret_type = &t_unknown;
1134 compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
1135
1136 // When the outer function is compiled for profiling or debugging, the
1137 // lambda may be called without profiling or debugging. Compile it here in
1138 // the right context.
1139 if (cctx->ctx_compile_type == CT_DEBUG
1140#ifdef FEAT_PROFILE
1141 || cctx->ctx_compile_type == CT_PROFILE
1142#endif
1143 )
1144 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1145
Bram Moolenaar139575d2022-03-15 19:29:30 +00001146 // if the outer function is not compiled for debugging or profiling, this
1147 // one might be
1148 if (cctx->ctx_compile_type == CT_NONE)
Bram Moolenaar96923b72022-03-15 15:57:04 +00001149 {
Bram Moolenaar139575d2022-03-15 19:29:30 +00001150 compiletype_T compile_type = get_compile_type(ufunc);
1151
1152 if (compile_type != CT_NONE)
1153 compile_def_function(ufunc, FALSE, compile_type, cctx);
Bram Moolenaar96923b72022-03-15 15:57:04 +00001154 }
1155
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001156 // The last entry in evalarg.eval_tofree_ga is a copy of the last line and
1157 // "*arg" may point into it. Point into the original line to avoid a
1158 // dangling pointer.
1159 if (evalarg.eval_using_cmdline)
1160 {
1161 garray_T *gap = &evalarg.eval_tofree_ga;
1162 size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
1163
1164 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
1165 + off;
Bram Moolenaarf8addf12022-09-23 12:44:25 +01001166 evalarg.eval_using_cmdline = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001167 }
1168
1169 clear_evalarg(&evalarg, NULL);
1170
1171 if (ufunc->uf_def_status == UF_COMPILED)
1172 {
1173 // The return type will now be known.
1174 set_function_type(ufunc);
1175
1176 // The function reference count will be 1. When the ISN_FUNCREF
1177 // instruction is deleted the reference count is decremented and the
1178 // function is freed.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001179 return generate_FUNCREF(cctx, ufunc, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001180 }
1181
1182 func_ptr_unref(ufunc);
1183 return FAIL;
1184}
1185
1186/*
1187 * Get a lambda and compile it. Uses Vim9 syntax.
1188 */
1189 int
1190get_lambda_tv_and_compile(
1191 char_u **arg,
1192 typval_T *rettv,
1193 int types_optional,
1194 evalarg_T *evalarg)
1195{
1196 int r;
1197 ufunc_T *ufunc;
1198 int save_sc_version = current_sctx.sc_version;
1199
1200 // Get the funcref in "rettv".
1201 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1202 r = get_lambda_tv(arg, rettv, types_optional, evalarg);
1203 current_sctx.sc_version = save_sc_version;
1204 if (r != OK)
Bram Moolenaar7f8a3b12022-05-12 22:03:01 +01001205 return r; // currently unreachable
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001206
1207 // "rettv" will now be a partial referencing the function.
1208 ufunc = rettv->vval.v_partial->pt_func;
1209
1210 // Compile it here to get the return type. The return type is optional,
1211 // when it's missing use t_unknown. This is recognized in
1212 // compile_return().
1213 if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
1214 ufunc->uf_ret_type = &t_unknown;
1215 compile_def_function(ufunc, FALSE, CT_NONE, NULL);
1216
1217 if (ufunc->uf_def_status == UF_COMPILED)
1218 {
1219 // The return type will now be known.
1220 set_function_type(ufunc);
1221 return OK;
1222 }
1223 clear_tv(rettv);
1224 return FAIL;
1225}
1226
1227/*
1228 * parse a dict: {key: val, [key]: val}
1229 * "*arg" points to the '{'.
1230 * ppconst->pp_is_const is set if all item values are a constant.
1231 */
1232 static int
1233compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1234{
1235 garray_T *instr = &cctx->ctx_instr;
1236 int count = 0;
1237 dict_T *d = dict_alloc();
1238 dictitem_T *item;
1239 char_u *whitep = *arg + 1;
1240 char_u *p;
1241 int is_const;
1242 int is_all_const = TRUE; // reset when non-const encountered
1243
1244 if (d == NULL)
1245 return FAIL;
1246 if (generate_ppconst(cctx, ppconst) == FAIL)
1247 return FAIL;
1248 for (;;)
1249 {
1250 char_u *key = NULL;
1251
1252 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1253 {
1254 *arg = NULL;
1255 goto failret;
1256 }
1257
1258 if (**arg == '}')
1259 break;
1260
1261 if (**arg == '[')
1262 {
1263 isn_T *isn;
1264
1265 // {[expr]: value} uses an evaluated key.
1266 *arg = skipwhite(*arg + 1);
1267 if (compile_expr0(arg, cctx) == FAIL)
1268 return FAIL;
1269 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1270 if (isn->isn_type == ISN_PUSHNR)
1271 {
1272 char buf[NUMBUFLEN];
1273
1274 // Convert to string at compile time.
1275 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
1276 isn->isn_type = ISN_PUSHS;
1277 isn->isn_arg.string = vim_strsave((char_u *)buf);
1278 }
1279 if (isn->isn_type == ISN_PUSHS)
1280 key = isn->isn_arg.string;
1281 else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
1282 return FAIL;
1283 *arg = skipwhite(*arg);
1284 if (**arg != ']')
1285 {
1286 emsg(_(e_missing_matching_bracket_after_dict_key));
1287 return FAIL;
1288 }
1289 ++*arg;
1290 }
1291 else
1292 {
1293 // {"name": value},
1294 // {'name': value},
1295 // {name: value} use "name" as a literal key
1296 key = get_literal_key(arg);
1297 if (key == NULL)
1298 return FAIL;
1299 if (generate_PUSHS(cctx, &key) == FAIL)
1300 return FAIL;
1301 }
1302
1303 // Check for duplicate keys, if using string keys.
1304 if (key != NULL)
1305 {
1306 item = dict_find(d, key, -1);
1307 if (item != NULL)
1308 {
dundargocc57b5bc2022-11-02 13:30:51 +00001309 semsg(_(e_duplicate_key_in_dictionary), key);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001310 goto failret;
1311 }
1312 item = dictitem_alloc(key);
1313 if (item != NULL)
1314 {
1315 item->di_tv.v_type = VAR_UNKNOWN;
1316 item->di_tv.v_lock = 0;
1317 if (dict_add(d, item) == FAIL)
1318 dictitem_free(item);
1319 }
1320 }
1321
1322 if (**arg != ':')
1323 {
1324 if (*skipwhite(*arg) == ':')
1325 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
1326 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00001327 semsg(_(e_missing_colon_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001328 return FAIL;
1329 }
1330 whitep = *arg + 1;
1331 if (!IS_WHITE_OR_NUL(*whitep))
1332 {
1333 semsg(_(e_white_space_required_after_str_str), ":", *arg);
1334 return FAIL;
1335 }
1336
1337 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1338 {
1339 *arg = NULL;
1340 goto failret;
1341 }
1342
1343 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
1344 return FAIL;
1345 if (!is_const)
1346 is_all_const = FALSE;
1347 ++count;
1348
1349 whitep = *arg;
1350 if (may_get_next_line(whitep, arg, cctx) == FAIL)
1351 {
1352 *arg = NULL;
1353 goto failret;
1354 }
1355 if (**arg == '}')
1356 break;
1357 if (**arg != ',')
1358 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001359 semsg(_(e_missing_comma_in_dictionary), *arg);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001360 goto failret;
1361 }
1362 if (IS_WHITE_OR_NUL(*whitep))
1363 {
1364 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
1365 return FAIL;
1366 }
1367 whitep = *arg + 1;
1368 if (!IS_WHITE_OR_NUL(*whitep))
1369 {
1370 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1371 return FAIL;
1372 }
1373 *arg = skipwhite(whitep);
1374 }
1375
1376 *arg = *arg + 1;
1377
1378 // Allow for following comment, after at least one space.
1379 p = skipwhite(*arg);
1380 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
1381 *arg += STRLEN(*arg);
1382
1383 dict_unref(d);
1384 ppconst->pp_is_const = is_all_const;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001385 return generate_NEWDICT(cctx, count, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001386
1387failret:
1388 if (*arg == NULL)
1389 {
1390 semsg(_(e_missing_dict_end), _("[end of lines]"));
1391 *arg = (char_u *)"";
1392 }
1393 dict_unref(d);
1394 return FAIL;
1395}
1396
1397/*
1398 * Compile "&option".
1399 */
1400 static int
1401compile_get_option(char_u **arg, cctx_T *cctx)
1402{
1403 typval_T rettv;
1404 char_u *start = *arg;
1405 int ret;
1406
1407 // parse the option and get the current value to get the type.
1408 rettv.v_type = VAR_UNKNOWN;
1409 ret = eval_option(arg, &rettv, TRUE);
1410 if (ret == OK)
1411 {
1412 // include the '&' in the name, eval_option() expects it.
1413 char_u *name = vim_strnsave(start, *arg - start);
1414 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
1415 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
1416
1417 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1418 vim_free(name);
1419 }
1420 clear_tv(&rettv);
1421
1422 return ret;
1423}
1424
1425/*
1426 * Compile "$VAR".
1427 */
1428 static int
1429compile_get_env(char_u **arg, cctx_T *cctx)
1430{
1431 char_u *start = *arg;
1432 int len;
1433 int ret;
1434 char_u *name;
1435
1436 ++*arg;
1437 len = get_env_len(arg);
1438 if (len == 0)
1439 {
Bram Moolenaar5f25c382022-01-09 13:36:28 +00001440 semsg(_(e_syntax_error_at_str), start);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001441 return FAIL;
1442 }
1443
1444 // include the '$' in the name, eval_env_var() expects it.
1445 name = vim_strnsave(start, len + 1);
1446 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
1447 vim_free(name);
1448 return ret;
1449}
1450
1451/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001452 * Compile $"string" or $'string'.
LemonBoy2eaef102022-05-06 13:14:50 +01001453 */
1454 static int
1455compile_interp_string(char_u **arg, cctx_T *cctx)
1456{
1457 typval_T tv;
1458 int ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001459 int quote;
LemonBoy2eaef102022-05-06 13:14:50 +01001460 int evaluate = cctx->ctx_skip != SKIP_YES;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001461 int count = 0;
1462 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01001463
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001464 // *arg is on the '$' character, move it to the first string character.
1465 ++*arg;
1466 quote = **arg;
1467 ++*arg;
LemonBoy2eaef102022-05-06 13:14:50 +01001468
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001469 for (;;)
1470 {
1471 // Get the string up to the matching quote or to a single '{'.
1472 // "arg" is advanced to either the quote or the '{'.
1473 if (quote == '"')
1474 ret = eval_string(arg, &tv, evaluate, TRUE);
1475 else
1476 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
1477 if (ret == FAIL)
1478 break;
1479 if (evaluate)
1480 {
1481 if ((tv.vval.v_string != NULL && *tv.vval.v_string != NUL)
1482 || (**arg != '{' && count == 0))
1483 {
1484 // generate non-empty string or empty string if it's the only
1485 // one
1486 if (generate_PUSHS(cctx, &tv.vval.v_string) == FAIL)
1487 return FAIL;
1488 tv.vval.v_string = NULL; // don't free it now
1489 ++count;
1490 }
1491 clear_tv(&tv);
1492 }
1493
1494 if (**arg != '{')
1495 {
1496 // found terminating quote
1497 ++*arg;
1498 break;
1499 }
1500
1501 p = compile_one_expr_in_str(*arg, cctx);
1502 if (p == NULL)
1503 {
1504 ret = FAIL;
1505 break;
1506 }
1507 ++count;
1508 *arg = p;
1509 }
LemonBoy2eaef102022-05-06 13:14:50 +01001510
1511 if (ret == FAIL || !evaluate)
1512 return ret;
1513
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001514 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
1515 if (count > 1)
1516 return generate_CONCAT(cctx, count);
LemonBoy2eaef102022-05-06 13:14:50 +01001517
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001518 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01001519}
1520
1521/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001522 * Compile "@r".
1523 */
1524 static int
1525compile_get_register(char_u **arg, cctx_T *cctx)
1526{
1527 int ret;
1528
1529 ++*arg;
1530 if (**arg == NUL)
1531 {
1532 semsg(_(e_syntax_error_at_str), *arg - 1);
1533 return FAIL;
1534 }
1535 if (!valid_yank_reg(**arg, FALSE))
1536 {
1537 emsg_invreg(**arg);
1538 return FAIL;
1539 }
1540 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
1541 ++*arg;
1542 return ret;
1543}
1544
1545/*
1546 * Apply leading '!', '-' and '+' to constant "rettv".
1547 * When "numeric_only" is TRUE do not apply '!'.
1548 */
1549 static int
1550apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
1551{
1552 char_u *p = *end;
1553
1554 // this works from end to start
1555 while (p > start)
1556 {
1557 --p;
1558 if (*p == '-' || *p == '+')
1559 {
1560 // only '-' has an effect, for '+' we only check the type
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001561 if (rettv->v_type == VAR_FLOAT)
1562 {
1563 if (*p == '-')
1564 rettv->vval.v_float = -rettv->vval.v_float;
1565 }
1566 else
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001567 {
1568 varnumber_T val;
1569 int error = FALSE;
1570
1571 // tv_get_number_chk() accepts a string, but we don't want that
1572 // here
1573 if (check_not_string(rettv) == FAIL)
1574 return FAIL;
1575 val = tv_get_number_chk(rettv, &error);
1576 clear_tv(rettv);
1577 if (error)
1578 return FAIL;
1579 if (*p == '-')
1580 val = -val;
1581 rettv->v_type = VAR_NUMBER;
1582 rettv->vval.v_number = val;
1583 }
1584 }
1585 else if (numeric_only)
1586 {
1587 ++p;
1588 break;
1589 }
1590 else if (*p == '!')
1591 {
1592 int v = tv2bool(rettv);
1593
1594 // '!' is permissive in the type.
1595 clear_tv(rettv);
1596 rettv->v_type = VAR_BOOL;
1597 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
1598 }
1599 }
1600 *end = p;
1601 return OK;
1602}
1603
1604/*
1605 * Recognize v: variables that are constants and set "rettv".
1606 */
1607 static void
1608get_vim_constant(char_u **arg, typval_T *rettv)
1609{
1610 if (STRNCMP(*arg, "v:true", 6) == 0)
1611 {
1612 rettv->v_type = VAR_BOOL;
1613 rettv->vval.v_number = VVAL_TRUE;
1614 *arg += 6;
1615 }
1616 else if (STRNCMP(*arg, "v:false", 7) == 0)
1617 {
1618 rettv->v_type = VAR_BOOL;
1619 rettv->vval.v_number = VVAL_FALSE;
1620 *arg += 7;
1621 }
1622 else if (STRNCMP(*arg, "v:null", 6) == 0)
1623 {
1624 rettv->v_type = VAR_SPECIAL;
1625 rettv->vval.v_number = VVAL_NULL;
1626 *arg += 6;
1627 }
1628 else if (STRNCMP(*arg, "v:none", 6) == 0)
1629 {
1630 rettv->v_type = VAR_SPECIAL;
1631 rettv->vval.v_number = VVAL_NONE;
1632 *arg += 6;
1633 }
1634}
1635
1636 exprtype_T
1637get_compare_type(char_u *p, int *len, int *type_is)
1638{
1639 exprtype_T type = EXPR_UNKNOWN;
1640 int i;
1641
1642 switch (p[0])
1643 {
1644 case '=': if (p[1] == '=')
1645 type = EXPR_EQUAL;
1646 else if (p[1] == '~')
1647 type = EXPR_MATCH;
1648 break;
1649 case '!': if (p[1] == '=')
1650 type = EXPR_NEQUAL;
1651 else if (p[1] == '~')
1652 type = EXPR_NOMATCH;
1653 break;
1654 case '>': if (p[1] != '=')
1655 {
1656 type = EXPR_GREATER;
1657 *len = 1;
1658 }
1659 else
1660 type = EXPR_GEQUAL;
1661 break;
1662 case '<': if (p[1] != '=')
1663 {
1664 type = EXPR_SMALLER;
1665 *len = 1;
1666 }
1667 else
1668 type = EXPR_SEQUAL;
1669 break;
1670 case 'i': if (p[1] == 's')
1671 {
1672 // "is" and "isnot"; but not a prefix of a name
1673 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
1674 *len = 5;
1675 i = p[*len];
1676 if (!isalnum(i) && i != '_')
1677 {
1678 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
1679 *type_is = TRUE;
1680 }
1681 }
1682 break;
1683 }
1684 return type;
1685}
1686
1687/*
1688 * Skip over an expression, ignoring most errors.
1689 */
1690 void
1691skip_expr_cctx(char_u **arg, cctx_T *cctx)
1692{
1693 evalarg_T evalarg;
1694
1695 init_evalarg(&evalarg);
1696 evalarg.eval_cctx = cctx;
1697 skip_expr(arg, &evalarg);
1698 clear_evalarg(&evalarg, NULL);
1699}
1700
1701/*
1702 * Check that the top of the type stack has a type that can be used as a
1703 * condition. Give an error and return FAIL if not.
1704 */
1705 int
1706bool_on_stack(cctx_T *cctx)
1707{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001708 type_T *type;
1709
Bram Moolenaar078a4612022-01-04 15:17:03 +00001710 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001711 if (type == &t_bool)
1712 return OK;
1713
Bram Moolenaar4913d422022-10-17 13:13:32 +01001714 if (type->tt_type == VAR_ANY
1715 || type->tt_type == VAR_UNKNOWN
1716 || type->tt_type == VAR_NUMBER
1717 || type == &t_number_bool
1718 || type == &t_const_number_bool)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001719 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
1720 // This requires a runtime type check.
1721 return generate_COND2BOOL(cctx);
1722
1723 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
1724}
1725
1726/*
1727 * Give the "white on both sides" error, taking the operator from "p[len]".
1728 */
1729 void
1730error_white_both(char_u *op, int len)
1731{
1732 char_u buf[10];
1733
1734 vim_strncpy(buf, op, len);
1735 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
1736}
1737
1738/*
1739 * Compile code to apply '-', '+' and '!'.
1740 * When "numeric_only" is TRUE do not apply '!'.
1741 */
1742 static int
1743compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
1744{
1745 char_u *p = *end;
1746
1747 // this works from end to start
1748 while (p > start)
1749 {
1750 --p;
1751 while (VIM_ISWHITE(*p))
1752 --p;
1753 if (*p == '-' || *p == '+')
1754 {
1755 int negate = *p == '-';
1756 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001757 type_T *type;
1758
Bram Moolenaar078a4612022-01-04 15:17:03 +00001759 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001760 if (type != &t_float && need_type(type, &t_number,
1761 -1, 0, cctx, FALSE, FALSE) == FAIL)
1762 return FAIL;
1763
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001764 // only '-' has an effect, for '+' we only check the type
1765 if (negate)
1766 {
1767 isn = generate_instr(cctx, ISN_NEGATENR);
1768 if (isn == NULL)
1769 return FAIL;
1770 }
1771 }
1772 else if (numeric_only)
1773 {
1774 ++p;
1775 break;
1776 }
1777 else
1778 {
1779 int invert = *p == '!';
1780
1781 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
1782 {
1783 if (p[-1] == '!')
1784 invert = !invert;
1785 --p;
1786 }
1787 if (generate_2BOOL(cctx, invert, -1) == FAIL)
1788 return FAIL;
1789 }
1790 }
1791 *end = p;
1792 return OK;
1793}
1794
1795/*
1796 * Compile "(expression)": recursive!
1797 * Return FAIL/OK.
1798 */
1799 static int
1800compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
1801{
1802 int ret;
1803 char_u *p = *arg + 1;
1804
1805 if (may_get_next_line_error(p, arg, cctx) == FAIL)
1806 return FAIL;
1807 if (ppconst->pp_used <= PPSIZE - 10)
1808 {
1809 ret = compile_expr1(arg, cctx, ppconst);
1810 }
1811 else
1812 {
1813 // Not enough space in ppconst, flush constants.
1814 if (generate_ppconst(cctx, ppconst) == FAIL)
1815 return FAIL;
1816 ret = compile_expr0(arg, cctx);
1817 }
1818 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
1819 return FAIL;
1820 if (**arg == ')')
1821 ++*arg;
1822 else if (ret == OK)
1823 {
1824 emsg(_(e_missing_closing_paren));
1825 ret = FAIL;
1826 }
1827 return ret;
1828}
1829
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001830static int compile_expr9(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
Bram Moolenaarc7349932022-01-16 20:59:39 +00001831
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001832/*
1833 * Compile whatever comes after "name" or "name()".
1834 * Advances "*arg" only when something was recognized.
1835 */
1836 static int
1837compile_subscript(
1838 char_u **arg,
1839 cctx_T *cctx,
1840 char_u *start_leader,
1841 char_u **end_leader,
1842 ppconst_T *ppconst)
1843{
1844 char_u *name_start = *end_leader;
1845 int keeping_dict = FALSE;
1846
1847 for (;;)
1848 {
1849 char_u *p = skipwhite(*arg);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001850 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001851
1852 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
1853 {
1854 char_u *next = peek_next_line_from_context(cctx);
1855
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001856 // If a following line starts with "->{", "->(" or "->X" advance to
1857 // that line, so that a line break before "->" is allowed.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001858 // Also if a following line starts with ".x".
1859 if (next != NULL &&
1860 ((next[0] == '-' && next[1] == '>'
1861 && (next[2] == '{'
Bram Moolenaard0fbb412022-10-19 18:04:49 +01001862 || next[2] == '('
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001863 || ASCII_ISALPHA(*skipwhite(next + 2))))
1864 || (next[0] == '.' && eval_isdictc(next[1]))))
1865 {
1866 next = next_line_from_context(cctx, TRUE);
1867 if (next == NULL)
1868 return FAIL;
1869 *arg = next;
1870 p = skipwhite(*arg);
1871 }
1872 }
1873
1874 // Do not skip over white space to find the "(", "execute 'x' (expr)"
1875 // is not a function call.
1876 if (**arg == '(')
1877 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001878 int argcount = 0;
1879
1880 if (generate_ppconst(cctx, ppconst) == FAIL)
1881 return FAIL;
1882 ppconst->pp_is_const = FALSE;
1883
1884 // funcref(arg)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001885 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001886
1887 *arg = skipwhite(p + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01001888 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001889 return FAIL;
1890 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
1891 return FAIL;
1892 if (keeping_dict)
1893 {
1894 keeping_dict = FALSE;
1895 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
1896 return FAIL;
1897 }
1898 }
1899 else if (*p == '-' && p[1] == '>')
1900 {
Bram Moolenaarc7349932022-01-16 20:59:39 +00001901 char_u *pstart = p;
1902 int alt;
1903 char_u *paren;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001904
Bram Moolenaarc7349932022-01-16 20:59:39 +00001905 // something->method()
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001906 if (generate_ppconst(cctx, ppconst) == FAIL)
1907 return FAIL;
1908 ppconst->pp_is_const = FALSE;
1909
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001910 // Apply the '!', '-' and '+' first:
1911 // -1.0->func() works like (-1.0)->func()
1912 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
1913 return FAIL;
1914
1915 p += 2;
1916 *arg = skipwhite(p);
1917 // No line break supported right after "->".
Bram Moolenaarc7349932022-01-16 20:59:39 +00001918
1919 // Three alternatives handled here:
1920 // 1. "base->name(" only a name, use compile_call()
1921 // 2. "base->(expr)(" evaluate "expr", then use PCALL
1922 // 3. "base->expr(" Same, find the end of "expr" by "("
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001923 if (**arg == '(')
Bram Moolenaarc7349932022-01-16 20:59:39 +00001924 alt = 2;
1925 else
1926 {
1927 // alternative 1 or 3
1928 p = *arg;
1929 if (!eval_isnamec1(*p))
1930 {
1931 semsg(_(e_trailing_characters_str), pstart);
1932 return FAIL;
1933 }
1934 if (ASCII_ISALPHA(*p) && p[1] == ':')
1935 p += 2;
1936 for ( ; eval_isnamec(*p); ++p)
1937 ;
1938 if (*p == '(')
1939 {
1940 // alternative 1
1941 alt = 1;
1942 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
1943 return FAIL;
1944 }
1945 else
1946 {
1947 // Must be alternative 3, find the "(". Only works within
1948 // one line.
1949 alt = 3;
1950 paren = vim_strchr(p, '(');
1951 if (paren == NULL)
1952 {
1953 semsg(_(e_missing_parenthesis_str), *arg);
1954 return FAIL;
1955 }
1956 }
1957 }
1958
1959 if (alt != 1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001960 {
1961 int argcount = 1;
1962 garray_T *stack = &cctx->ctx_type_stack;
1963 int type_idx_start = stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001964 int expr_isn_start = cctx->ctx_instr.ga_len;
1965 int expr_isn_end;
1966 int arg_isn_count;
1967
Bram Moolenaarc7349932022-01-16 20:59:39 +00001968 if (alt == 2)
1969 {
1970 // Funcref call: list->(Refs[2])(arg)
1971 // or lambda: list->((arg) => expr)(arg)
1972 //
1973 // Fist compile the function expression.
1974 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
1975 return FAIL;
1976 }
1977 else
1978 {
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001979 int fail;
1980 int save_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001981 int prev_did_emsg = did_emsg;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001982
Bram Moolenaarc7349932022-01-16 20:59:39 +00001983 *paren = NUL;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001984
1985 // instead of using LOADG for "import.Func" use PUSHFUNC
1986 ++paren_follows_after_expr;
1987
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001988 // do not look in the next line
1989 cctx->ctx_ufunc->uf_lines.ga_len = 1;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001990
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01001991 fail = compile_expr9(arg, cctx, ppconst) == FAIL
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001992 || *skipwhite(*arg) != NUL;
1993 *paren = '(';
Bram Moolenaard02dce22022-01-18 17:43:04 +00001994 --paren_follows_after_expr;
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001995 cctx->ctx_ufunc->uf_lines.ga_len = save_len;
Bram Moolenaard02dce22022-01-18 17:43:04 +00001996
Bram Moolenaar6389baa2022-01-17 20:50:40 +00001997 if (fail)
Bram Moolenaarc7349932022-01-16 20:59:39 +00001998 {
Bram Moolenaar31ad32a2022-05-13 16:23:37 +01001999 if (did_emsg == prev_did_emsg)
2000 semsg(_(e_invalid_expression_str), pstart);
Bram Moolenaarc7349932022-01-16 20:59:39 +00002001 return FAIL;
2002 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002003 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002004
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002005 // Compile the arguments.
2006 if (**arg != '(')
2007 {
2008 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002009 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002010 else
2011 semsg(_(e_missing_parenthesis_str), *arg);
2012 return FAIL;
2013 }
Bram Moolenaar6389baa2022-01-17 20:50:40 +00002014
2015 // Remember the next instruction index, where the instructions
2016 // for arguments are being written.
2017 expr_isn_end = cctx->ctx_instr.ga_len;
2018
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002019 *arg = skipwhite(*arg + 1);
LemonBoyf3b48952022-05-05 13:53:03 +01002020 if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL)
2021 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002022 return FAIL;
2023
2024 // Move the instructions for the arguments to before the
2025 // instructions of the expression and move the type of the
2026 // expression after the argument types. This is what ISN_PCALL
2027 // expects.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002028 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
2029 if (arg_isn_count > 0)
2030 {
2031 int expr_isn_count = expr_isn_end - expr_isn_start;
2032 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002033 type_T *decl_type;
2034 type2_T *typep;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002035
2036 if (isn == NULL)
2037 return FAIL;
2038 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
2039 + expr_isn_start,
2040 sizeof(isn_T) * expr_isn_count);
2041 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2042 + expr_isn_start,
2043 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
2044 sizeof(isn_T) * arg_isn_count);
2045 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
2046 + expr_isn_start + arg_isn_count,
2047 isn, sizeof(isn_T) * expr_isn_count);
2048 vim_free(isn);
2049
Bram Moolenaar078a4612022-01-04 15:17:03 +00002050 typep = ((type2_T *)stack->ga_data) + type_idx_start;
2051 type = typep->type_curr;
2052 decl_type = typep->type_decl;
2053 mch_memmove(((type2_T *)stack->ga_data) + type_idx_start,
2054 ((type2_T *)stack->ga_data) + type_idx_start + 1,
2055 sizeof(type2_T)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002056 * (stack->ga_len - type_idx_start - 1));
Bram Moolenaar078a4612022-01-04 15:17:03 +00002057 typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1;
2058 typep->type_curr = type;
2059 typep->type_decl = decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002060 }
2061
Bram Moolenaar078a4612022-01-04 15:17:03 +00002062 type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002063 if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
2064 return FAIL;
2065 }
Bram Moolenaarc7349932022-01-16 20:59:39 +00002066
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002067 if (keeping_dict)
2068 {
2069 keeping_dict = FALSE;
2070 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2071 return FAIL;
2072 }
2073 }
2074 else if (**arg == '[')
2075 {
2076 int is_slice = FALSE;
2077
2078 // list index: list[123]
2079 // dict member: dict[key]
2080 // string index: text[123]
2081 // blob index: blob[123]
2082 if (generate_ppconst(cctx, ppconst) == FAIL)
2083 return FAIL;
2084 ppconst->pp_is_const = FALSE;
2085
2086 ++p;
2087 if (may_get_next_line_error(p, arg, cctx) == FAIL)
2088 return FAIL;
2089 if (**arg == ':')
2090 {
2091 // missing first index is equal to zero
2092 generate_PUSHNR(cctx, 0);
2093 }
2094 else
2095 {
2096 if (compile_expr0(arg, cctx) == FAIL)
2097 return FAIL;
2098 if (**arg == ':')
2099 {
2100 semsg(_(e_white_space_required_before_and_after_str_at_str),
2101 ":", *arg);
2102 return FAIL;
2103 }
2104 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2105 return FAIL;
2106 *arg = skipwhite(*arg);
2107 }
2108 if (**arg == ':')
2109 {
2110 is_slice = TRUE;
2111 ++*arg;
2112 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
2113 {
2114 semsg(_(e_white_space_required_before_and_after_str_at_str),
2115 ":", *arg);
2116 return FAIL;
2117 }
2118 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2119 return FAIL;
2120 if (**arg == ']')
2121 // missing second index is equal to end of string
2122 generate_PUSHNR(cctx, -1);
2123 else
2124 {
2125 if (compile_expr0(arg, cctx) == FAIL)
2126 return FAIL;
2127 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2128 return FAIL;
2129 *arg = skipwhite(*arg);
2130 }
2131 }
2132
2133 if (**arg != ']')
2134 {
2135 emsg(_(e_missing_closing_square_brace));
2136 return FAIL;
2137 }
2138 *arg = *arg + 1;
2139
2140 if (keeping_dict)
2141 {
2142 keeping_dict = FALSE;
2143 if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
2144 return FAIL;
2145 }
2146 if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
2147 return FAIL;
2148 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002149 else if (*p == '.'
2150 && (type = get_type_on_stack(cctx, 0)) != &t_unknown
2151 && (type->tt_type == VAR_CLASS || type->tt_type == VAR_OBJECT))
2152 {
2153 // class member: SomeClass.varname
2154 // class method: SomeClass.SomeMethod()
2155 // class constructor: SomeClass.new()
2156 // object member: someObject.varname, this.varname
2157 // object method: someObject.SomeMethod(), this.SomeMethod()
2158 if (compile_class_object_index(cctx, arg, type) == FAIL)
2159 return FAIL;
2160 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002161 else if (*p == '.' && p[1] != '.')
2162 {
2163 // dictionary member: dict.name
2164 if (generate_ppconst(cctx, ppconst) == FAIL)
2165 return FAIL;
2166 ppconst->pp_is_const = FALSE;
2167
2168 *arg = p + 1;
2169 if (IS_WHITE_OR_NUL(**arg))
2170 {
2171 emsg(_(e_missing_name_after_dot));
2172 return FAIL;
2173 }
2174 p = *arg;
2175 if (eval_isdictc(*p))
2176 while (eval_isnamec(*p))
2177 MB_PTR_ADV(p);
2178 if (p == *arg)
2179 {
2180 semsg(_(e_syntax_error_at_str), *arg);
2181 return FAIL;
2182 }
2183 if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
2184 return FAIL;
2185 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
2186 return FAIL;
2187 keeping_dict = TRUE;
2188 *arg = p;
2189 }
2190 else
2191 break;
2192 }
2193
2194 // Turn "dict.Func" into a partial for "Func" bound to "dict".
2195 // This needs to be done at runtime to be able to check the type.
Bram Moolenaar1ff9c442022-05-17 15:03:33 +01002196 if (keeping_dict && cctx->ctx_skip != SKIP_YES
2197 && generate_instr(cctx, ISN_USEDICT) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002198 return FAIL;
2199
2200 return OK;
2201}
2202
2203/*
2204 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
2205 * "arg" is advanced until after the expression, skipping white space.
2206 *
2207 * If the value is a constant "ppconst->pp_used" will be non-zero.
2208 * Before instructions are generated, any values in "ppconst" will generated.
2209 *
2210 * This is the compiling equivalent of eval1(), eval2(), etc.
2211 */
2212
2213/*
2214 * number number constant
2215 * 0zFFFFFFFF Blob constant
2216 * "string" string constant
2217 * 'string' literal string constant
2218 * &option-name option value
2219 * @r register contents
2220 * identifier variable value
2221 * function() function call
2222 * $VAR environment variable
2223 * (expression) nested expression
2224 * [expr, expr] List
2225 * {key: val, [key]: val} Dictionary
2226 *
2227 * Also handle:
2228 * ! in front logical NOT
2229 * - in front unary minus
2230 * + in front unary plus (ignored)
2231 * trailing (arg) funcref/partial call
2232 * trailing [] subscript in String or List
2233 * trailing .name entry in Dictionary
2234 * trailing ->name() method call
2235 */
2236 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002237compile_expr9(
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002238 char_u **arg,
2239 cctx_T *cctx,
2240 ppconst_T *ppconst)
2241{
2242 char_u *start_leader, *end_leader;
2243 int ret = OK;
2244 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
2245 int used_before = ppconst->pp_used;
2246
2247 ppconst->pp_is_const = FALSE;
2248
2249 /*
2250 * Skip '!', '-' and '+' characters. They are handled later.
2251 */
2252 start_leader = *arg;
2253 if (eval_leader(arg, TRUE) == FAIL)
2254 return FAIL;
2255 end_leader = *arg;
2256
2257 rettv->v_type = VAR_UNKNOWN;
2258 switch (**arg)
2259 {
2260 /*
2261 * Number constant.
2262 */
2263 case '0': // also for blob starting with 0z
2264 case '1':
2265 case '2':
2266 case '3':
2267 case '4':
2268 case '5':
2269 case '6':
2270 case '7':
2271 case '8':
2272 case '9':
2273 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
2274 return FAIL;
2275 // Apply "-" and "+" just before the number now, right to
2276 // left. Matters especially when "->" follows. Stops at
2277 // '!'.
2278 if (apply_leader(rettv, TRUE,
2279 start_leader, &end_leader) == FAIL)
2280 {
2281 clear_tv(rettv);
2282 return FAIL;
2283 }
2284 break;
2285
2286 /*
2287 * String constant: "string".
2288 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002289 case '"': if (eval_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002290 return FAIL;
2291 break;
2292
2293 /*
2294 * Literal string constant: 'str''ing'.
2295 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002296 case '\'': if (eval_lit_string(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002297 return FAIL;
2298 break;
2299
2300 /*
2301 * Constant Vim variable.
2302 */
2303 case 'v': get_vim_constant(arg, rettv);
2304 ret = NOTDONE;
2305 break;
2306
2307 /*
2308 * "true" constant
2309 */
2310 case 't': if (STRNCMP(*arg, "true", 4) == 0
2311 && !eval_isnamec((*arg)[4]))
2312 {
2313 *arg += 4;
2314 rettv->v_type = VAR_BOOL;
2315 rettv->vval.v_number = VVAL_TRUE;
2316 }
2317 else
2318 ret = NOTDONE;
2319 break;
2320
2321 /*
2322 * "false" constant
2323 */
2324 case 'f': if (STRNCMP(*arg, "false", 5) == 0
2325 && !eval_isnamec((*arg)[5]))
2326 {
2327 *arg += 5;
2328 rettv->v_type = VAR_BOOL;
2329 rettv->vval.v_number = VVAL_FALSE;
2330 }
2331 else
2332 ret = NOTDONE;
2333 break;
2334
2335 /*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002336 * "null" or "null_*" constant
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002337 */
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002338 case 'n': if (STRNCMP(*arg, "null", 4) == 0)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002339 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002340 char_u *p = *arg + 4;
2341 int len;
2342
2343 for (len = 0; eval_isnamec(p[len]); ++len)
2344 ;
2345 ret = handle_predefined(*arg, len + 4, rettv);
2346 if (ret == FAIL)
2347 ret = NOTDONE;
2348 else
2349 *arg += len + 4;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002350 }
2351 else
2352 ret = NOTDONE;
2353 break;
2354
2355 /*
2356 * List: [expr, expr]
2357 */
2358 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
2359 return FAIL;
2360 ret = compile_list(arg, cctx, ppconst);
2361 break;
2362
2363 /*
2364 * Dictionary: {'key': val, 'key': val}
2365 */
2366 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
2367 return FAIL;
2368 ret = compile_dict(arg, cctx, ppconst);
2369 break;
2370
2371 /*
2372 * Option value: &name
2373 */
2374 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
2375 return FAIL;
2376 ret = compile_get_option(arg, cctx);
2377 break;
2378
2379 /*
2380 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01002381 * Interpolated string: $"string" or $'string'.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002382 */
2383 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
2384 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01002385 if ((*arg)[1] == '"' || (*arg)[1] == '\'')
2386 ret = compile_interp_string(arg, cctx);
2387 else
2388 ret = compile_get_env(arg, cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002389 break;
2390
2391 /*
2392 * Register contents: @r.
2393 */
2394 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
2395 return FAIL;
2396 ret = compile_get_register(arg, cctx);
2397 break;
2398 /*
2399 * nested expression: (expression).
2400 * lambda: (arg, arg) => expr
2401 * funcref: (arg, arg) => { statement }
2402 */
2403 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
2404 ret = compile_lambda(arg, cctx);
2405 if (ret == NOTDONE)
2406 ret = compile_parenthesis(arg, cctx, ppconst);
2407 break;
2408
2409 default: ret = NOTDONE;
2410 break;
2411 }
2412 if (ret == FAIL)
2413 return FAIL;
2414
2415 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
2416 {
2417 if (cctx->ctx_skip == SKIP_YES)
2418 clear_tv(rettv);
2419 else
2420 // A constant expression can possibly be handled compile time,
2421 // return the value instead of generating code.
2422 ++ppconst->pp_used;
2423 }
2424 else if (ret == NOTDONE)
2425 {
2426 char_u *p;
2427 int r;
2428
2429 if (!eval_isnamec1(**arg))
2430 {
2431 if (!vim9_bad_comment(*arg))
2432 {
2433 if (ends_excmd(*skipwhite(*arg)))
2434 semsg(_(e_empty_expression_str), *arg);
2435 else
2436 semsg(_(e_name_expected_str), *arg);
2437 }
2438 return FAIL;
2439 }
2440
2441 // "name" or "name()"
2442 p = to_name_end(*arg, TRUE);
2443 if (p - *arg == (size_t)1 && **arg == '_')
2444 {
2445 emsg(_(e_cannot_use_underscore_here));
2446 return FAIL;
2447 }
2448
2449 if (*p == '(')
2450 {
2451 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
2452 }
2453 else
2454 {
2455 if (cctx->ctx_skip != SKIP_YES
2456 && generate_ppconst(cctx, ppconst) == FAIL)
2457 return FAIL;
2458 r = compile_load(arg, p, cctx, TRUE, TRUE);
2459 }
2460 if (r == FAIL)
2461 return FAIL;
2462 }
2463
2464 // Handle following "[]", ".member", etc.
2465 // Then deal with prefixed '-', '+' and '!', if not done already.
2466 if (compile_subscript(arg, cctx, start_leader, &end_leader,
2467 ppconst) == FAIL)
2468 return FAIL;
2469 if (ppconst->pp_used > 0)
2470 {
2471 // apply the '!', '-' and '+' before the constant
2472 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
2473 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
2474 return FAIL;
2475 return OK;
2476 }
2477 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
2478 return FAIL;
2479 return OK;
2480}
2481
2482/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002483 * <type>expr9: runtime type check / conversion
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002484 */
2485 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002486compile_expr8(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002487{
2488 type_T *want_type = NULL;
2489
2490 // Recognize <type>
2491 if (**arg == '<' && eval_isnamec1((*arg)[1]))
2492 {
2493 ++*arg;
2494 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
2495 if (want_type == NULL)
2496 return FAIL;
2497
2498 if (**arg != '>')
2499 {
2500 if (*skipwhite(*arg) == '>')
2501 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
2502 else
2503 emsg(_(e_missing_gt));
2504 return FAIL;
2505 }
2506 ++*arg;
2507 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
2508 return FAIL;
2509 }
2510
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002511 if (compile_expr9(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002512 return FAIL;
2513
2514 if (want_type != NULL)
2515 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002516 type_T *actual;
2517 where_T where = WHERE_INIT;
2518
2519 generate_ppconst(cctx, ppconst);
Bram Moolenaar078a4612022-01-04 15:17:03 +00002520 actual = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002521 if (check_type_maybe(want_type, actual, FALSE, where) != OK)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002522 {
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002523 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE)
2524 == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002525 return FAIL;
2526 }
2527 }
2528
2529 return OK;
2530}
2531
2532/*
2533 * * number multiplication
2534 * / number division
2535 * % number modulo
2536 */
2537 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002538compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002539{
2540 char_u *op;
2541 char_u *next;
2542 int ppconst_used = ppconst->pp_used;
2543
2544 // get the first expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002545 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002546 return FAIL;
2547
2548 /*
2549 * Repeat computing, until no "*", "/" or "%" is following.
2550 */
2551 for (;;)
2552 {
2553 op = may_peek_next_line(cctx, *arg, &next);
2554 if (*op != '*' && *op != '/' && *op != '%')
2555 break;
2556 if (next != NULL)
2557 {
2558 *arg = next_line_from_context(cctx, TRUE);
2559 op = skipwhite(*arg);
2560 }
2561
2562 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
2563 {
2564 error_white_both(op, 1);
2565 return FAIL;
2566 }
2567 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
2568 return FAIL;
2569
2570 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002571 if (compile_expr8(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002572 return FAIL;
2573
2574 if (ppconst->pp_used == ppconst_used + 2
2575 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2576 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
2577 {
2578 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2579 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2580 varnumber_T res = 0;
2581 int failed = FALSE;
2582
2583 // both are numbers: compute the result
2584 switch (*op)
2585 {
2586 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
2587 break;
2588 case '/': res = num_divide(tv1->vval.v_number,
2589 tv2->vval.v_number, &failed);
2590 break;
2591 case '%': res = num_modulus(tv1->vval.v_number,
2592 tv2->vval.v_number, &failed);
2593 break;
2594 }
2595 if (failed)
2596 return FAIL;
2597 tv1->vval.v_number = res;
2598 --ppconst->pp_used;
2599 }
2600 else
2601 {
2602 generate_ppconst(cctx, ppconst);
2603 generate_two_op(cctx, op);
2604 }
2605 }
2606
2607 return OK;
2608}
2609
2610/*
2611 * + number addition or list/blobl concatenation
2612 * - number subtraction
2613 * .. string concatenation
2614 */
2615 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002616compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002617{
2618 char_u *op;
2619 char_u *next;
2620 int oplen;
2621 int ppconst_used = ppconst->pp_used;
2622
2623 // get the first variable
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002624 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002625 return FAIL;
2626
2627 /*
2628 * Repeat computing, until no "+", "-" or ".." is following.
2629 */
2630 for (;;)
2631 {
2632 op = may_peek_next_line(cctx, *arg, &next);
2633 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
2634 break;
2635 if (op[0] == op[1] && *op != '.' && next)
2636 // Finding "++" or "--" on the next line is a separate command.
2637 // But ".." is concatenation.
2638 break;
2639 oplen = (*op == '.' ? 2 : 1);
2640 if (next != NULL)
2641 {
2642 *arg = next_line_from_context(cctx, TRUE);
2643 op = skipwhite(*arg);
2644 }
2645
2646 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
2647 {
2648 error_white_both(op, oplen);
2649 return FAIL;
2650 }
2651
2652 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
2653 return FAIL;
2654
2655 // get the second expression
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002656 if (compile_expr7(arg, cctx, ppconst) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002657 return FAIL;
2658
2659 if (ppconst->pp_used == ppconst_used + 2
2660 && (*op == '.'
2661 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
2662 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
2663 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
2664 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
2665 {
2666 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
2667 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
2668
2669 // concat/subtract/add constant numbers
2670 if (*op == '+')
2671 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
2672 else if (*op == '-')
2673 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
2674 else
2675 {
2676 // concatenate constant strings
2677 char_u *s1 = tv1->vval.v_string;
2678 char_u *s2 = tv2->vval.v_string;
2679 size_t len1 = STRLEN(s1);
2680
2681 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
2682 if (tv1->vval.v_string == NULL)
2683 {
2684 clear_ppconst(ppconst);
2685 return FAIL;
2686 }
2687 mch_memmove(tv1->vval.v_string, s1, len1);
2688 STRCPY(tv1->vval.v_string + len1, s2);
2689 vim_free(s1);
2690 vim_free(s2);
2691 }
2692 --ppconst->pp_used;
2693 }
2694 else
2695 {
2696 generate_ppconst(cctx, ppconst);
2697 ppconst->pp_is_const = FALSE;
2698 if (*op == '.')
2699 {
2700 if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
2701 || may_generate_2STRING(-1, FALSE, cctx) == FAIL)
2702 return FAIL;
LemonBoy372bcce2022-04-25 12:43:20 +01002703 if (generate_CONCAT(cctx, 2) == FAIL)
2704 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002705 }
2706 else
2707 generate_two_op(cctx, op);
2708 }
2709 }
2710
2711 return OK;
2712}
2713
2714/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002715 * expr6a >> expr6b
2716 * expr6a << expr6b
2717 *
2718 * Produces instructions:
2719 * OPNR bitwise left or right shift
2720 */
2721 static int
2722compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2723{
2724 exprtype_T type = EXPR_UNKNOWN;
2725 char_u *p;
2726 char_u *next;
2727 int len = 2;
2728 int ppconst_used = ppconst->pp_used;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002729 isn_T *isn;
2730
2731 // get the first variable
2732 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2733 return FAIL;
2734
2735 /*
2736 * Repeat computing, until no "+", "-" or ".." is following.
2737 */
2738 for (;;)
2739 {
2740 type = EXPR_UNKNOWN;
2741
2742 p = may_peek_next_line(cctx, *arg, &next);
2743 if (p[0] == '<' && p[1] == '<')
2744 type = EXPR_LSHIFT;
2745 else if (p[0] == '>' && p[1] == '>')
2746 type = EXPR_RSHIFT;
2747
2748 if (type == EXPR_UNKNOWN)
2749 return OK;
2750
2751 // Handle a bitwise left or right shift operator
2752 if (ppconst->pp_used == ppconst_used + 1)
2753 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002754 if (ppconst->pp_tv[ppconst->pp_used - 1].v_type != VAR_NUMBER)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002755 {
2756 // left operand should be a number
2757 emsg(_(e_bitshift_ops_must_be_number));
2758 return FAIL;
2759 }
2760 }
2761 else
2762 {
2763 type_T *t = get_type_on_stack(cctx, 0);
2764
2765 if (need_type(t, &t_number, 0, 0, cctx, FALSE, FALSE) == FAIL)
2766 {
2767 emsg(_(e_bitshift_ops_must_be_number));
2768 return FAIL;
2769 }
2770 }
2771
2772 if (next != NULL)
2773 {
2774 *arg = next_line_from_context(cctx, TRUE);
2775 p = skipwhite(*arg);
2776 }
2777
2778 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2779 {
2780 error_white_both(p, len);
2781 return FAIL;
2782 }
2783
2784 // get the second variable
2785 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2786 return FAIL;
2787
2788 if (compile_expr6(arg, cctx, ppconst) == FAIL)
2789 return FAIL;
2790
2791 if (ppconst->pp_used == ppconst_used + 2)
2792 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002793 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
2794 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2795
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002796 // Both sides are a constant, compute the result now.
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002797 if (tv2->v_type != VAR_NUMBER || tv2->vval.v_number < 0)
2798 {
2799 // right operand should be a positive number
2800 if (tv2->v_type != VAR_NUMBER)
2801 emsg(_(e_bitshift_ops_must_be_number));
2802 else
dundargocc57b5bc2022-11-02 13:30:51 +00002803 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002804 return FAIL;
2805 }
2806
2807 if (tv2->vval.v_number > MAX_LSHIFT_BITS)
2808 tv1->vval.v_number = 0;
2809 else if (type == EXPR_LSHIFT)
Bram Moolenaar68e64d22022-05-22 22:07:52 +01002810 tv1->vval.v_number =
2811 (uvarnumber_T)tv1->vval.v_number << tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002812 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01002813 tv1->vval.v_number =
2814 (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002815 clear_tv(tv2);
2816 --ppconst->pp_used;
2817 }
2818 else
2819 {
2820 if (need_type(get_type_on_stack(cctx, 0), &t_number, 0, 0, cctx,
2821 FALSE, FALSE) == FAIL)
2822 {
2823 emsg(_(e_bitshift_ops_must_be_number));
2824 return FAIL;
2825 }
2826
2827 generate_ppconst(cctx, ppconst);
2828
2829 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
2830 if (isn == NULL)
2831 return FAIL;
2832
2833 if (isn != NULL)
2834 isn->isn_arg.op.op_type = type;
2835 }
2836 }
2837
2838 return OK;
2839}
2840
2841/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002842 * expr5a == expr5b
2843 * expr5a =~ expr5b
2844 * expr5a != expr5b
2845 * expr5a !~ expr5b
2846 * expr5a > expr5b
2847 * expr5a >= expr5b
2848 * expr5a < expr5b
2849 * expr5a <= expr5b
2850 * expr5a is expr5b
2851 * expr5a isnot expr5b
2852 *
2853 * Produces instructions:
2854 * EVAL expr5a Push result of "expr5a"
2855 * EVAL expr5b Push result of "expr5b"
2856 * COMPARE one of the compare instructions
2857 */
2858 static int
2859compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2860{
2861 exprtype_T type = EXPR_UNKNOWN;
2862 char_u *p;
2863 char_u *next;
2864 int len = 2;
2865 int type_is = FALSE;
2866 int ppconst_used = ppconst->pp_used;
2867
2868 // get the first variable
2869 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2870 return FAIL;
2871
2872 p = may_peek_next_line(cctx, *arg, &next);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002873
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002874 type = get_compare_type(p, &len, &type_is);
2875
2876 /*
2877 * If there is a comparative operator, use it.
2878 */
2879 if (type != EXPR_UNKNOWN)
2880 {
2881 int ic = FALSE; // Default: do not ignore case
2882
2883 if (next != NULL)
2884 {
2885 *arg = next_line_from_context(cctx, TRUE);
2886 p = skipwhite(*arg);
2887 }
2888 if (type_is && (p[len] == '?' || p[len] == '#'))
2889 {
2890 semsg(_(e_invalid_expression_str), *arg);
2891 return FAIL;
2892 }
2893 // extra question mark appended: ignore case
2894 if (p[len] == '?')
2895 {
2896 ic = TRUE;
2897 ++len;
2898 }
2899 // extra '#' appended: match case (ignored)
2900 else if (p[len] == '#')
2901 ++len;
2902 // nothing appended: match case
2903
2904 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
2905 {
2906 error_white_both(p, len);
2907 return FAIL;
2908 }
2909
2910 // get the second variable
2911 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
2912 return FAIL;
2913
2914 if (compile_expr5(arg, cctx, ppconst) == FAIL)
2915 return FAIL;
2916
2917 if (ppconst->pp_used == ppconst_used + 2)
2918 {
Bram Moolenaar5b529232022-05-22 21:53:26 +01002919 typval_T *tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002920 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
2921 int ret;
2922
2923 // Both sides are a constant, compute the result now.
2924 // First check for a valid combination of types, this is more
2925 // strict than typval_compare().
2926 if (check_compare_types(type, tv1, tv2) == FAIL)
2927 ret = FAIL;
2928 else
2929 {
2930 ret = typval_compare(tv1, tv2, type, ic);
2931 tv1->v_type = VAR_BOOL;
2932 tv1->vval.v_number = tv1->vval.v_number
2933 ? VVAL_TRUE : VVAL_FALSE;
2934 clear_tv(tv2);
2935 --ppconst->pp_used;
2936 }
2937 return ret;
2938 }
2939
2940 generate_ppconst(cctx, ppconst);
2941 return generate_COMPARE(cctx, type, ic);
2942 }
2943
2944 return OK;
2945}
2946
2947static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2948
2949/*
2950 * Compile || or &&.
2951 */
2952 static int
2953compile_and_or(
2954 char_u **arg,
2955 cctx_T *cctx,
2956 char *op,
2957 ppconst_T *ppconst,
2958 int ppconst_used UNUSED)
2959{
2960 char_u *next;
2961 char_u *p = may_peek_next_line(cctx, *arg, &next);
2962 int opchar = *op;
2963
2964 if (p[0] == opchar && p[1] == opchar)
2965 {
2966 garray_T *instr = &cctx->ctx_instr;
2967 garray_T end_ga;
2968 int save_skip = cctx->ctx_skip;
2969
2970 /*
2971 * Repeat until there is no following "||" or "&&"
2972 */
2973 ga_init2(&end_ga, sizeof(int), 10);
2974 while (p[0] == opchar && p[1] == opchar)
2975 {
2976 long start_lnum = SOURCING_LNUM;
2977 long save_sourcing_lnum;
2978 int start_ctx_lnum = cctx->ctx_lnum;
2979 int save_lnum;
2980 int const_used;
2981 int status;
2982 jumpwhen_T jump_when = opchar == '|'
2983 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
2984
2985 if (next != NULL)
2986 {
2987 *arg = next_line_from_context(cctx, TRUE);
2988 p = skipwhite(*arg);
2989 }
2990
2991 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
2992 {
2993 semsg(_(e_white_space_required_before_and_after_str_at_str),
2994 op, p);
2995 ga_clear(&end_ga);
2996 return FAIL;
2997 }
2998
2999 save_sourcing_lnum = SOURCING_LNUM;
3000 SOURCING_LNUM = start_lnum;
3001 save_lnum = cctx->ctx_lnum;
3002 cctx->ctx_lnum = start_ctx_lnum;
3003
3004 status = check_ppconst_bool(ppconst);
3005 if (status != FAIL)
3006 {
3007 // Use the last ppconst if possible.
3008 if (ppconst->pp_used > 0)
3009 {
3010 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3011 int is_true = tv2bool(tv);
3012
3013 if ((is_true && opchar == '|')
3014 || (!is_true && opchar == '&'))
3015 {
3016 // For "false && expr" and "true || expr" the "expr"
3017 // does not need to be evaluated.
3018 cctx->ctx_skip = SKIP_YES;
3019 clear_tv(tv);
3020 tv->v_type = VAR_BOOL;
3021 tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
3022 }
3023 else
3024 {
3025 // For "true && expr" and "false || expr" only "expr"
3026 // needs to be evaluated.
3027 --ppconst->pp_used;
3028 jump_when = JUMP_NEVER;
3029 }
3030 }
3031 else
3032 {
3033 // Every part must evaluate to a bool.
3034 status = bool_on_stack(cctx);
3035 }
3036 }
3037 if (status != FAIL)
3038 status = ga_grow(&end_ga, 1);
3039 cctx->ctx_lnum = save_lnum;
3040 if (status == FAIL)
3041 {
3042 ga_clear(&end_ga);
3043 return FAIL;
3044 }
3045
3046 if (jump_when != JUMP_NEVER)
3047 {
3048 if (cctx->ctx_skip != SKIP_YES)
3049 {
3050 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
3051 ++end_ga.ga_len;
3052 }
3053 generate_JUMP(cctx, jump_when, 0);
3054 }
3055
3056 // eval the next expression
3057 SOURCING_LNUM = save_sourcing_lnum;
3058 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
3059 {
3060 ga_clear(&end_ga);
3061 return FAIL;
3062 }
3063
3064 const_used = ppconst->pp_used;
3065 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
3066 : compile_expr4(arg, cctx, ppconst)) == FAIL)
3067 {
3068 ga_clear(&end_ga);
3069 return FAIL;
3070 }
3071
3072 // "0 || 1" results in true, "1 && 0" results in false.
3073 if (ppconst->pp_used == const_used + 1)
3074 {
3075 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
3076
3077 if (tv->v_type == VAR_NUMBER
3078 && (tv->vval.v_number == 1 || tv->vval.v_number == 0))
3079 {
3080 tv->vval.v_number = tv->vval.v_number == 1
3081 ? VVAL_TRUE : VVAL_FALSE;
3082 tv->v_type = VAR_BOOL;
3083 }
3084 }
3085
3086 p = may_peek_next_line(cctx, *arg, &next);
3087 }
3088
3089 if (check_ppconst_bool(ppconst) == FAIL)
3090 {
3091 ga_clear(&end_ga);
3092 return FAIL;
3093 }
3094
3095 if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
3096 // Every part must evaluate to a bool.
3097 if (bool_on_stack(cctx) == FAIL)
3098 {
3099 ga_clear(&end_ga);
3100 return FAIL;
3101 }
3102
3103 if (end_ga.ga_len > 0)
3104 {
3105 // Fill in the end label in all jumps.
3106 generate_ppconst(cctx, ppconst);
3107 while (end_ga.ga_len > 0)
3108 {
3109 isn_T *isn;
3110
3111 --end_ga.ga_len;
3112 isn = ((isn_T *)instr->ga_data)
3113 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
3114 isn->isn_arg.jump.jump_where = instr->ga_len;
3115 }
3116 }
3117 ga_clear(&end_ga);
3118
3119 cctx->ctx_skip = save_skip;
3120 }
3121
3122 return OK;
3123}
3124
3125/*
3126 * expr4a && expr4a && expr4a logical AND
3127 *
3128 * Produces instructions:
3129 * EVAL expr4a Push result of "expr4a"
3130 * COND2BOOL convert to bool if needed
3131 * JUMP_IF_COND_FALSE end
3132 * EVAL expr4b Push result of "expr4b"
3133 * JUMP_IF_COND_FALSE end
3134 * EVAL expr4c Push result of "expr4c"
3135 * end:
3136 */
3137 static int
3138compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3139{
3140 int ppconst_used = ppconst->pp_used;
3141
3142 // get the first variable
3143 if (compile_expr4(arg, cctx, ppconst) == FAIL)
3144 return FAIL;
3145
3146 // || and && work almost the same
3147 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
3148}
3149
3150/*
3151 * expr3a || expr3b || expr3c logical OR
3152 *
3153 * Produces instructions:
3154 * EVAL expr3a Push result of "expr3a"
3155 * COND2BOOL convert to bool if needed
3156 * JUMP_IF_COND_TRUE end
3157 * EVAL expr3b Push result of "expr3b"
3158 * JUMP_IF_COND_TRUE end
3159 * EVAL expr3c Push result of "expr3c"
3160 * end:
3161 */
3162 static int
3163compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3164{
3165 int ppconst_used = ppconst->pp_used;
3166
3167 // eval the first expression
3168 if (compile_expr3(arg, cctx, ppconst) == FAIL)
3169 return FAIL;
3170
3171 // || and && work almost the same
3172 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
3173}
3174
3175/*
3176 * Toplevel expression: expr2 ? expr1a : expr1b
3177 * Produces instructions:
3178 * EVAL expr2 Push result of "expr2"
3179 * JUMP_IF_FALSE alt jump if false
3180 * EVAL expr1a
3181 * JUMP_ALWAYS end
3182 * alt: EVAL expr1b
3183 * end:
3184 *
3185 * Toplevel expression: expr2 ?? expr1
3186 * Produces instructions:
3187 * EVAL expr2 Push result of "expr2"
3188 * JUMP_AND_KEEP_IF_TRUE end jump if true
3189 * EVAL expr1
3190 * end:
3191 */
3192 int
3193compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3194{
3195 char_u *p;
3196 int ppconst_used = ppconst->pp_used;
3197 char_u *next;
3198
3199 // Ignore all kinds of errors when not producing code.
3200 if (cctx->ctx_skip == SKIP_YES)
3201 {
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003202 int prev_did_emsg = did_emsg;
3203
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003204 skip_expr_cctx(arg, cctx);
Bram Moolenaar83d0cec2022-02-04 21:17:58 +00003205 return did_emsg == prev_did_emsg ? OK : FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003206 }
3207
3208 // Evaluate the first expression.
3209 if (compile_expr2(arg, cctx, ppconst) == FAIL)
3210 return FAIL;
3211
3212 p = may_peek_next_line(cctx, *arg, &next);
3213 if (*p == '?')
3214 {
3215 int op_falsy = p[1] == '?';
3216 garray_T *instr = &cctx->ctx_instr;
3217 garray_T *stack = &cctx->ctx_type_stack;
3218 int alt_idx = instr->ga_len;
3219 int end_idx = 0;
3220 isn_T *isn;
3221 type_T *type1 = NULL;
3222 int has_const_expr = FALSE;
3223 int const_value = FALSE;
3224 int save_skip = cctx->ctx_skip;
3225
3226 if (next != NULL)
3227 {
3228 *arg = next_line_from_context(cctx, TRUE);
3229 p = skipwhite(*arg);
3230 }
3231
3232 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
3233 {
3234 semsg(_(e_white_space_required_before_and_after_str_at_str),
3235 op_falsy ? "??" : "?", p);
3236 return FAIL;
3237 }
3238
3239 if (ppconst->pp_used == ppconst_used + 1)
3240 {
3241 // the condition is a constant, we know whether the ? or the :
3242 // expression is to be evaluated.
3243 has_const_expr = TRUE;
3244 if (op_falsy)
3245 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
3246 else
3247 {
3248 int error = FALSE;
3249
3250 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
3251 &error);
3252 if (error)
3253 return FAIL;
3254 }
3255 cctx->ctx_skip = save_skip == SKIP_YES ||
3256 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
3257
3258 if (op_falsy && cctx->ctx_skip == SKIP_YES)
3259 // "left ?? right" and "left" is truthy: produce "left"
3260 generate_ppconst(cctx, ppconst);
3261 else
3262 {
3263 clear_tv(&ppconst->pp_tv[ppconst_used]);
3264 --ppconst->pp_used;
3265 }
3266 }
3267 else
3268 {
3269 generate_ppconst(cctx, ppconst);
3270 if (op_falsy)
3271 end_idx = instr->ga_len;
3272 generate_JUMP(cctx, op_falsy
3273 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
3274 if (op_falsy)
Bram Moolenaar078a4612022-01-04 15:17:03 +00003275 type1 = get_type_on_stack(cctx, -1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003276 }
3277
3278 // evaluate the second expression; any type is accepted
3279 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
3280 return FAIL;
3281 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3282 return FAIL;
3283
3284 if (!has_const_expr)
3285 {
3286 generate_ppconst(cctx, ppconst);
3287
3288 if (!op_falsy)
3289 {
3290 // remember the type and drop it
Bram Moolenaar078a4612022-01-04 15:17:03 +00003291 type1 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003292 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003293
3294 end_idx = instr->ga_len;
3295 generate_JUMP(cctx, JUMP_ALWAYS, 0);
3296
3297 // jump here from JUMP_IF_FALSE
3298 isn = ((isn_T *)instr->ga_data) + alt_idx;
3299 isn->isn_arg.jump.jump_where = instr->ga_len;
3300 }
3301 }
3302
3303 if (!op_falsy)
3304 {
3305 // Check for the ":".
3306 p = may_peek_next_line(cctx, *arg, &next);
3307 if (*p != ':')
3308 {
3309 emsg(_(e_missing_colon_after_questionmark));
3310 return FAIL;
3311 }
3312 if (next != NULL)
3313 {
3314 *arg = next_line_from_context(cctx, TRUE);
3315 p = skipwhite(*arg);
3316 }
3317
3318 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
3319 {
3320 semsg(_(e_white_space_required_before_and_after_str_at_str),
3321 ":", p);
3322 return FAIL;
3323 }
3324
3325 // evaluate the third expression
3326 if (has_const_expr)
3327 cctx->ctx_skip = save_skip == SKIP_YES || const_value
3328 ? SKIP_YES : SKIP_NOT;
3329 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
3330 return FAIL;
3331 if (compile_expr1(arg, cctx, ppconst) == FAIL)
3332 return FAIL;
3333 }
3334
3335 if (!has_const_expr)
3336 {
3337 type_T **typep;
3338
3339 generate_ppconst(cctx, ppconst);
Bram Moolenaarfa46ead2021-12-22 13:18:39 +00003340 ppconst->pp_is_const = FALSE;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003341
3342 // If the types differ, the result has a more generic type.
Bram Moolenaar078a4612022-01-04 15:17:03 +00003343 typep = &((((type2_T *)stack->ga_data)
3344 + stack->ga_len - 1)->type_curr);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00003345 common_type(type1, *typep, typep, cctx->ctx_type_list);
3346
3347 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
3348 isn = ((isn_T *)instr->ga_data) + end_idx;
3349 isn->isn_arg.jump.jump_where = instr->ga_len;
3350 }
3351
3352 cctx->ctx_skip = save_skip;
3353 }
3354 return OK;
3355}
3356
3357/*
3358 * Toplevel expression.
3359 * Sets "is_const" (if not NULL) to indicate the value is a constant.
3360 * Returns OK or FAIL.
3361 */
3362 int
3363compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
3364{
3365 ppconst_T ppconst;
3366
3367 CLEAR_FIELD(ppconst);
3368 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
3369 {
3370 clear_ppconst(&ppconst);
3371 return FAIL;
3372 }
3373 if (is_const != NULL)
3374 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
3375 if (generate_ppconst(cctx, &ppconst) == FAIL)
3376 return FAIL;
3377 return OK;
3378}
3379
3380/*
3381 * Toplevel expression.
3382 */
3383 int
3384compile_expr0(char_u **arg, cctx_T *cctx)
3385{
3386 return compile_expr0_ext(arg, cctx, NULL);
3387}
3388
3389
3390#endif // defined(FEAT_EVAL)