Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1 | /* 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 | /* |
| 11 | * vim9cmds.c: Dealing with compiled function expressions |
| 12 | */ |
| 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 | |
| 24 | /* |
| 25 | * Generate code for any ppconst entries. |
| 26 | */ |
| 27 | int |
| 28 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 29 | { |
| 30 | int i; |
| 31 | int ret = OK; |
| 32 | int save_skip = cctx->ctx_skip; |
| 33 | |
| 34 | cctx->ctx_skip = SKIP_NOT; |
| 35 | for (i = 0; i < ppconst->pp_used; ++i) |
| 36 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 37 | ret = FAIL; |
| 38 | ppconst->pp_used = 0; |
| 39 | cctx->ctx_skip = save_skip; |
| 40 | return ret; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * Check that the last item of "ppconst" is a bool, if there is an item. |
| 45 | */ |
| 46 | static int |
| 47 | check_ppconst_bool(ppconst_T *ppconst) |
| 48 | { |
| 49 | if (ppconst->pp_used > 0) |
| 50 | { |
| 51 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 52 | where_T where = WHERE_INIT; |
| 53 | |
| 54 | return check_typval_type(&t_bool, tv, where); |
| 55 | } |
| 56 | return OK; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Clear ppconst constants. Used when failing. |
| 61 | */ |
| 62 | void |
| 63 | clear_ppconst(ppconst_T *ppconst) |
| 64 | { |
| 65 | int i; |
| 66 | |
| 67 | for (i = 0; i < ppconst->pp_used; ++i) |
| 68 | clear_tv(&ppconst->pp_tv[i]); |
| 69 | ppconst->pp_used = 0; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Compile getting a member from a list/dict/string/blob. Stack has the |
| 74 | * indexable value and the index or the two indexes of a slice. |
| 75 | * "keeping_dict" is used for dict[func](arg) to pass dict to func. |
| 76 | */ |
| 77 | int |
| 78 | compile_member(int is_slice, int *keeping_dict, cctx_T *cctx) |
| 79 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 80 | type2_T *typep; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 81 | garray_T *stack = &cctx->ctx_type_stack; |
| 82 | vartype_T vartype; |
| 83 | type_T *idxtype; |
| 84 | |
| 85 | // We can index a list, dict and blob. If we don't know the type |
| 86 | // we can use the index value type. If we still don't know use an "ANY" |
| 87 | // instruction. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 88 | // TODO: what about the decl type? |
| 89 | typep = (((type2_T *)stack->ga_data) + stack->ga_len - (is_slice ? 3 : 2)); |
| 90 | vartype = typep->type_curr->tt_type; |
| 91 | idxtype = (((type2_T *)stack->ga_data) + stack->ga_len - 1)->type_curr; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 92 | // If the index is a string, the variable must be a Dict. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 93 | if ((typep->type_curr == &t_any || typep->type_curr == &t_unknown) |
| 94 | && idxtype == &t_string) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 95 | vartype = VAR_DICT; |
| 96 | if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB) |
| 97 | { |
| 98 | if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL) |
| 99 | return FAIL; |
| 100 | if (is_slice) |
| 101 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 102 | idxtype = get_type_on_stack(cctx, 1); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 103 | if (need_type(idxtype, &t_number, -2, 0, cctx, |
| 104 | FALSE, FALSE) == FAIL) |
| 105 | return FAIL; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (vartype == VAR_DICT) |
| 110 | { |
| 111 | if (is_slice) |
| 112 | { |
| 113 | emsg(_(e_cannot_slice_dictionary)); |
| 114 | return FAIL; |
| 115 | } |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 116 | if (typep->type_curr->tt_type == VAR_DICT) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 117 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 118 | typep->type_curr = typep->type_curr->tt_member; |
| 119 | if (typep->type_curr == &t_unknown) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 120 | // empty dict was used |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 121 | typep->type_curr = &t_any; |
| 122 | if (typep->type_decl->tt_type == VAR_DICT) |
| 123 | { |
| 124 | typep->type_decl = typep->type_decl->tt_member; |
| 125 | if (typep->type_decl == &t_unknown) |
| 126 | // empty dict was used |
| 127 | typep->type_decl = &t_any; |
| 128 | } |
| 129 | else |
| 130 | typep->type_decl = typep->type_curr; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 131 | } |
| 132 | else |
| 133 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 134 | if (need_type(typep->type_curr, &t_dict_any, -2, 0, cctx, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 135 | FALSE, FALSE) == FAIL) |
| 136 | return FAIL; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 137 | typep->type_curr = &t_any; |
| 138 | typep->type_decl = &t_any; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 139 | } |
| 140 | if (may_generate_2STRING(-1, FALSE, cctx) == FAIL) |
| 141 | return FAIL; |
| 142 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 143 | return FAIL; |
| 144 | if (keeping_dict != NULL) |
| 145 | *keeping_dict = TRUE; |
| 146 | } |
| 147 | else if (vartype == VAR_STRING) |
| 148 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 149 | typep->type_curr = &t_string; |
| 150 | typep->type_decl = &t_string; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 151 | if ((is_slice |
| 152 | ? generate_instr_drop(cctx, ISN_STRSLICE, 2) |
| 153 | : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL) |
| 154 | return FAIL; |
| 155 | } |
| 156 | else if (vartype == VAR_BLOB) |
| 157 | { |
| 158 | if (is_slice) |
| 159 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 160 | typep->type_curr = &t_blob; |
| 161 | typep->type_decl = &t_blob; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 162 | if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL) |
| 163 | return FAIL; |
| 164 | } |
| 165 | else |
| 166 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 167 | typep->type_curr = &t_number; |
| 168 | typep->type_decl = &t_number; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 169 | if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL) |
| 170 | return FAIL; |
| 171 | } |
| 172 | } |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 173 | else if (vartype == VAR_LIST || typep->type_curr == &t_any |
| 174 | || typep->type_curr == &t_unknown) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 175 | { |
| 176 | if (is_slice) |
| 177 | { |
| 178 | if (generate_instr_drop(cctx, |
| 179 | vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE, |
| 180 | 2) == FAIL) |
| 181 | return FAIL; |
| 182 | } |
| 183 | else |
| 184 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 185 | if (typep->type_curr->tt_type == VAR_LIST) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 186 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 187 | typep->type_curr = typep->type_curr->tt_member; |
| 188 | if (typep->type_curr == &t_unknown) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 189 | // empty list was used |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 190 | typep->type_curr = &t_any; |
| 191 | if (typep->type_decl->tt_type == VAR_LIST) |
| 192 | { |
| 193 | typep->type_decl = typep->type_decl->tt_member; |
| 194 | if (typep->type_decl == &t_unknown) |
| 195 | // empty list was used |
| 196 | typep->type_decl = &t_any; |
| 197 | } |
| 198 | else |
| 199 | typep->type_decl = typep->type_curr; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 200 | } |
| 201 | if (generate_instr_drop(cctx, |
| 202 | vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) |
| 203 | == FAIL) |
| 204 | return FAIL; |
| 205 | } |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | switch (vartype) |
| 210 | { |
| 211 | case VAR_FUNC: |
| 212 | case VAR_PARTIAL: |
| 213 | emsg(_(e_cannot_index_a_funcref)); |
| 214 | break; |
| 215 | case VAR_BOOL: |
| 216 | case VAR_SPECIAL: |
| 217 | case VAR_JOB: |
| 218 | case VAR_CHANNEL: |
| 219 | case VAR_INSTR: |
| 220 | case VAR_UNKNOWN: |
| 221 | case VAR_ANY: |
| 222 | case VAR_VOID: |
| 223 | emsg(_(e_cannot_index_special_variable)); |
| 224 | break; |
| 225 | default: |
| 226 | emsg(_(e_string_list_dict_or_blob_required)); |
| 227 | } |
| 228 | return FAIL; |
| 229 | } |
| 230 | return OK; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Generate an instruction to load script-local variable "name", without the |
| 235 | * leading "s:". |
| 236 | * Also finds imported variables. |
| 237 | */ |
| 238 | int |
| 239 | compile_load_scriptvar( |
| 240 | cctx_T *cctx, |
| 241 | char_u *name, // variable NUL terminated |
| 242 | char_u *start, // start of variable |
| 243 | char_u **end, // end of variable |
| 244 | int error) // when TRUE may give error |
| 245 | { |
| 246 | scriptitem_T *si; |
| 247 | int idx; |
| 248 | imported_T *import; |
| 249 | |
| 250 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
| 251 | return FAIL; |
| 252 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
| 253 | idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx); |
| 254 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
| 255 | { |
| 256 | // variable is not in sn_var_vals: old style script. |
| 257 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 258 | &t_any); |
| 259 | } |
| 260 | if (idx >= 0) |
| 261 | { |
| 262 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 263 | |
| 264 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 265 | current_sctx.sc_sid, idx, sv->sv_type); |
| 266 | return OK; |
| 267 | } |
| 268 | |
| 269 | import = find_imported(name, 0, cctx); |
| 270 | if (import != NULL) |
| 271 | { |
| 272 | if (import->imp_flags & IMP_FLAGS_STAR) |
| 273 | { |
| 274 | char_u *p = skipwhite(*end); |
| 275 | char_u *exp_name; |
| 276 | int cc; |
| 277 | ufunc_T *ufunc; |
| 278 | type_T *type; |
| 279 | |
| 280 | // Used "import * as Name", need to lookup the member. |
| 281 | if (*p != '.') |
| 282 | { |
| 283 | semsg(_(e_expected_dot_after_name_str), start); |
| 284 | return FAIL; |
| 285 | } |
| 286 | ++p; |
| 287 | if (VIM_ISWHITE(*p)) |
| 288 | { |
| 289 | emsg(_(e_no_white_space_allowed_after_dot)); |
| 290 | return FAIL; |
| 291 | } |
| 292 | |
| 293 | // isolate one name |
| 294 | exp_name = p; |
| 295 | while (eval_isnamec(*p)) |
| 296 | ++p; |
| 297 | cc = *p; |
| 298 | *p = NUL; |
| 299 | |
| 300 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, |
| 301 | cctx, TRUE); |
| 302 | *p = cc; |
| 303 | p = skipwhite(p); |
| 304 | *end = p; |
| 305 | |
| 306 | if (idx < 0) |
| 307 | { |
| 308 | if (*p == '(' && ufunc != NULL) |
| 309 | { |
| 310 | generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type); |
| 311 | return OK; |
| 312 | } |
| 313 | return FAIL; |
| 314 | } |
| 315 | |
| 316 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 317 | import->imp_sid, |
| 318 | idx, |
| 319 | type); |
| 320 | } |
| 321 | else if (import->imp_funcname != NULL) |
| 322 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
| 323 | else |
| 324 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 325 | import->imp_sid, |
| 326 | import->imp_var_vals_idx, |
| 327 | import->imp_type); |
| 328 | return OK; |
| 329 | } |
| 330 | |
| 331 | if (error) |
| 332 | semsg(_(e_item_not_found_str), name); |
| 333 | return FAIL; |
| 334 | } |
| 335 | |
| 336 | static int |
| 337 | generate_funcref(cctx_T *cctx, char_u *name) |
| 338 | { |
| 339 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
| 340 | |
| 341 | if (ufunc == NULL) |
| 342 | return FAIL; |
| 343 | |
| 344 | // Need to compile any default values to get the argument types. |
| 345 | if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)) |
| 346 | && compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL) |
| 347 | == FAIL) |
| 348 | return FAIL; |
| 349 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Compile a variable name into a load instruction. |
| 354 | * "end" points to just after the name. |
| 355 | * "is_expr" is TRUE when evaluating an expression, might be a funcref. |
| 356 | * When "error" is FALSE do not give an error when not found. |
| 357 | */ |
| 358 | int |
| 359 | compile_load( |
| 360 | char_u **arg, |
| 361 | char_u *end_arg, |
| 362 | cctx_T *cctx, |
| 363 | int is_expr, |
| 364 | int error) |
| 365 | { |
| 366 | type_T *type; |
| 367 | char_u *name = NULL; |
| 368 | char_u *end = end_arg; |
| 369 | int res = FAIL; |
| 370 | int prev_called_emsg = called_emsg; |
| 371 | |
| 372 | if (*(*arg + 1) == ':') |
| 373 | { |
| 374 | if (end <= *arg + 2) |
| 375 | { |
| 376 | isntype_T isn_type; |
| 377 | |
| 378 | // load dictionary of namespace |
| 379 | switch (**arg) |
| 380 | { |
| 381 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 382 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 383 | case 't': isn_type = ISN_LOADTDICT; break; |
| 384 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 385 | default: |
| 386 | semsg(_(e_namespace_not_supported_str), *arg); |
| 387 | goto theend; |
| 388 | } |
| 389 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 390 | goto theend; |
| 391 | res = OK; |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | isntype_T isn_type = ISN_DROP; |
| 396 | |
| 397 | // load namespaced variable |
| 398 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 399 | if (name == NULL) |
| 400 | return FAIL; |
| 401 | |
| 402 | switch (**arg) |
| 403 | { |
| 404 | case 'v': res = generate_LOADV(cctx, name, error); |
| 405 | break; |
| 406 | case 's': if (is_expr && ASCII_ISUPPER(*name) |
| 407 | && find_func(name, FALSE, cctx) != NULL) |
| 408 | res = generate_funcref(cctx, name); |
| 409 | else |
| 410 | res = compile_load_scriptvar(cctx, name, |
| 411 | NULL, &end, error); |
| 412 | break; |
| 413 | case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL) |
| 414 | { |
| 415 | if (is_expr && ASCII_ISUPPER(*name) |
| 416 | && find_func(name, FALSE, cctx) != NULL) |
| 417 | res = generate_funcref(cctx, name); |
| 418 | else |
| 419 | isn_type = ISN_LOADG; |
| 420 | } |
| 421 | else |
| 422 | { |
| 423 | isn_type = ISN_LOADAUTO; |
| 424 | vim_free(name); |
| 425 | name = vim_strnsave(*arg, end - *arg); |
| 426 | if (name == NULL) |
| 427 | return FAIL; |
| 428 | } |
| 429 | break; |
| 430 | case 'w': isn_type = ISN_LOADW; break; |
| 431 | case 't': isn_type = ISN_LOADT; break; |
| 432 | case 'b': isn_type = ISN_LOADB; break; |
| 433 | default: // cannot happen, just in case |
| 434 | semsg(_(e_namespace_not_supported_str), *arg); |
| 435 | goto theend; |
| 436 | } |
| 437 | if (isn_type != ISN_DROP) |
| 438 | { |
| 439 | // Global, Buffer-local, Window-local and Tabpage-local |
| 440 | // variables can be defined later, thus we don't check if it |
| 441 | // exists, give an error at runtime. |
Bram Moolenaar | fa46ead | 2021-12-22 13:18:39 +0000 | [diff] [blame] | 442 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | size_t len = end - *arg; |
| 449 | int idx; |
| 450 | int gen_load = FALSE; |
| 451 | int gen_load_outer = 0; |
| 452 | |
| 453 | name = vim_strnsave(*arg, end - *arg); |
| 454 | if (name == NULL) |
| 455 | return FAIL; |
| 456 | |
| 457 | if (vim_strchr(name, AUTOLOAD_CHAR) != NULL) |
| 458 | { |
| 459 | script_autoload(name, FALSE); |
| 460 | res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any); |
| 461 | } |
| 462 | else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) |
| 463 | == OK) |
| 464 | { |
| 465 | if (gen_load_outer == 0) |
| 466 | gen_load = TRUE; |
| 467 | } |
| 468 | else |
| 469 | { |
| 470 | lvar_T lvar; |
| 471 | |
| 472 | if (lookup_local(*arg, len, &lvar, cctx) == OK) |
| 473 | { |
| 474 | type = lvar.lv_type; |
| 475 | idx = lvar.lv_idx; |
| 476 | if (lvar.lv_from_outer != 0) |
| 477 | gen_load_outer = lvar.lv_from_outer; |
| 478 | else |
| 479 | gen_load = TRUE; |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | // "var" can be script-local even without using "s:" if it |
| 484 | // already exists in a Vim9 script or when it's imported. |
| 485 | if (script_var_exists(*arg, len, cctx) == OK |
| 486 | || find_imported(name, 0, cctx) != NULL) |
| 487 | res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE); |
| 488 | |
| 489 | // When evaluating an expression and the name starts with an |
| 490 | // uppercase letter it can be a user defined function. |
| 491 | // generate_funcref() will fail if the function can't be found. |
| 492 | if (res == FAIL && is_expr && ASCII_ISUPPER(*name)) |
| 493 | res = generate_funcref(cctx, name); |
| 494 | } |
| 495 | } |
| 496 | if (gen_load) |
| 497 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
| 498 | if (gen_load_outer > 0) |
| 499 | { |
| 500 | res = generate_LOADOUTER(cctx, idx, gen_load_outer, type); |
| 501 | cctx->ctx_outer_used = TRUE; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | *arg = end; |
| 506 | |
| 507 | theend: |
| 508 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
| 509 | semsg(_(e_variable_not_found_str), name); |
| 510 | vim_free(name); |
| 511 | return res; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Compile a string in a ISN_PUSHS instruction into an ISN_INSTR. |
| 516 | * Returns FAIL if compilation fails. |
| 517 | */ |
| 518 | static int |
| 519 | compile_string(isn_T *isn, cctx_T *cctx) |
| 520 | { |
| 521 | char_u *s = isn->isn_arg.string; |
| 522 | garray_T save_ga = cctx->ctx_instr; |
| 523 | int expr_res; |
| 524 | int trailing_error; |
| 525 | int instr_count; |
| 526 | isn_T *instr = NULL; |
| 527 | |
| 528 | // Remove the string type from the stack. |
| 529 | --cctx->ctx_type_stack.ga_len; |
| 530 | |
| 531 | // Temporarily reset the list of instructions so that the jump labels are |
| 532 | // correct. |
| 533 | cctx->ctx_instr.ga_len = 0; |
| 534 | cctx->ctx_instr.ga_maxlen = 0; |
| 535 | cctx->ctx_instr.ga_data = NULL; |
| 536 | expr_res = compile_expr0(&s, cctx); |
| 537 | s = skipwhite(s); |
| 538 | trailing_error = *s != NUL; |
| 539 | |
| 540 | if (expr_res == FAIL || trailing_error |
| 541 | || GA_GROW_FAILS(&cctx->ctx_instr, 1)) |
| 542 | { |
| 543 | if (trailing_error) |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 544 | semsg(_(e_trailing_characters_str), s); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 545 | clear_instr_ga(&cctx->ctx_instr); |
| 546 | cctx->ctx_instr = save_ga; |
| 547 | ++cctx->ctx_type_stack.ga_len; |
| 548 | return FAIL; |
| 549 | } |
| 550 | |
| 551 | // Move the generated instructions into the ISN_INSTR instruction, then |
| 552 | // restore the list of instructions. |
| 553 | instr_count = cctx->ctx_instr.ga_len; |
| 554 | instr = cctx->ctx_instr.ga_data; |
| 555 | instr[instr_count].isn_type = ISN_FINISH; |
| 556 | |
| 557 | cctx->ctx_instr = save_ga; |
| 558 | vim_free(isn->isn_arg.string); |
| 559 | isn->isn_type = ISN_INSTR; |
| 560 | isn->isn_arg.instr = instr; |
| 561 | return OK; |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | * Compile the argument expressions. |
| 566 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 567 | */ |
| 568 | static int |
| 569 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair) |
| 570 | { |
| 571 | char_u *p = *arg; |
| 572 | char_u *whitep = *arg; |
| 573 | int must_end = FALSE; |
| 574 | int instr_count; |
| 575 | |
| 576 | for (;;) |
| 577 | { |
| 578 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 579 | goto failret; |
| 580 | if (*p == ')') |
| 581 | { |
| 582 | *arg = p + 1; |
| 583 | return OK; |
| 584 | } |
| 585 | if (must_end) |
| 586 | { |
| 587 | semsg(_(e_missing_comma_before_argument_str), p); |
| 588 | return FAIL; |
| 589 | } |
| 590 | |
| 591 | instr_count = cctx->ctx_instr.ga_len; |
| 592 | if (compile_expr0(&p, cctx) == FAIL) |
| 593 | return FAIL; |
| 594 | ++*argcount; |
| 595 | |
| 596 | if (is_searchpair && *argcount == 5 |
| 597 | && cctx->ctx_instr.ga_len == instr_count + 1) |
| 598 | { |
| 599 | isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count; |
| 600 | |
| 601 | // {skip} argument of searchpair() can be compiled if not empty |
| 602 | if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL) |
| 603 | compile_string(isn, cctx); |
| 604 | } |
| 605 | |
| 606 | if (*p != ',' && *skipwhite(p) == ',') |
| 607 | { |
| 608 | semsg(_(e_no_white_space_allowed_before_str_str), ",", p); |
| 609 | p = skipwhite(p); |
| 610 | } |
| 611 | if (*p == ',') |
| 612 | { |
| 613 | ++p; |
| 614 | if (*p != NUL && !VIM_ISWHITE(*p)) |
| 615 | semsg(_(e_white_space_required_after_str_str), ",", p - 1); |
| 616 | } |
| 617 | else |
| 618 | must_end = TRUE; |
| 619 | whitep = p; |
| 620 | p = skipwhite(p); |
| 621 | } |
| 622 | failret: |
| 623 | emsg(_(e_missing_closing_paren)); |
| 624 | return FAIL; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Compile a function call: name(arg1, arg2) |
| 629 | * "arg" points to "name", "arg + varlen" to the "(". |
| 630 | * "argcount_init" is 1 for "value->method()" |
| 631 | * Instructions: |
| 632 | * EVAL arg1 |
| 633 | * EVAL arg2 |
| 634 | * BCALL / DCALL / UCALL |
| 635 | */ |
| 636 | static int |
| 637 | compile_call( |
| 638 | char_u **arg, |
| 639 | size_t varlen, |
| 640 | cctx_T *cctx, |
| 641 | ppconst_T *ppconst, |
| 642 | int argcount_init) |
| 643 | { |
| 644 | char_u *name = *arg; |
| 645 | char_u *p; |
| 646 | int argcount = argcount_init; |
| 647 | char_u namebuf[100]; |
| 648 | char_u fname_buf[FLEN_FIXED + 1]; |
| 649 | char_u *tofree = NULL; |
| 650 | int error = FCERR_NONE; |
| 651 | ufunc_T *ufunc = NULL; |
| 652 | int res = FAIL; |
| 653 | int is_autoload; |
| 654 | int is_searchpair; |
| 655 | |
| 656 | // We can evaluate "has('name')" at compile time. |
| 657 | // We always evaluate "exists_compiled()" at compile time. |
| 658 | if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 659 | || (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0)) |
| 660 | { |
| 661 | char_u *s = skipwhite(*arg + varlen + 1); |
| 662 | typval_T argvars[2]; |
| 663 | int is_has = **arg == 'h'; |
| 664 | |
| 665 | argvars[0].v_type = VAR_UNKNOWN; |
| 666 | if (*s == '"') |
| 667 | (void)eval_string(&s, &argvars[0], TRUE); |
| 668 | else if (*s == '\'') |
| 669 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
| 670 | s = skipwhite(s); |
| 671 | if (*s == ')' && argvars[0].v_type == VAR_STRING |
| 672 | && ((is_has && !dynamic_feature(argvars[0].vval.v_string)) |
| 673 | || !is_has)) |
| 674 | { |
| 675 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 676 | |
| 677 | *arg = s + 1; |
| 678 | argvars[1].v_type = VAR_UNKNOWN; |
| 679 | tv->v_type = VAR_NUMBER; |
| 680 | tv->vval.v_number = 0; |
| 681 | if (is_has) |
| 682 | f_has(argvars, tv); |
| 683 | else |
| 684 | f_exists(argvars, tv); |
| 685 | clear_tv(&argvars[0]); |
| 686 | ++ppconst->pp_used; |
| 687 | return OK; |
| 688 | } |
| 689 | clear_tv(&argvars[0]); |
| 690 | if (!is_has) |
| 691 | { |
| 692 | emsg(_(e_argument_of_exists_compiled_must_be_literal_string)); |
| 693 | return FAIL; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 698 | return FAIL; |
| 699 | |
| 700 | if (varlen >= sizeof(namebuf)) |
| 701 | { |
| 702 | semsg(_(e_name_too_long_str), name); |
| 703 | return FAIL; |
| 704 | } |
| 705 | vim_strncpy(namebuf, *arg, varlen); |
| 706 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
| 707 | |
| 708 | // We handle the "skip" argument of searchpair() and searchpairpos() |
| 709 | // differently. |
| 710 | is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0) |
| 711 | || (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0) |
| 712 | || (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0) |
| 713 | || (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0); |
| 714 | |
| 715 | *arg = skipwhite(*arg + varlen + 1); |
| 716 | if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL) |
| 717 | goto theend; |
| 718 | |
| 719 | is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL; |
| 720 | if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload) |
| 721 | { |
| 722 | int idx; |
| 723 | |
| 724 | // builtin function |
| 725 | idx = find_internal_func(name); |
| 726 | if (idx >= 0) |
| 727 | { |
| 728 | if (STRCMP(name, "flatten") == 0) |
| 729 | { |
| 730 | emsg(_(e_cannot_use_flatten_in_vim9_script)); |
| 731 | goto theend; |
| 732 | } |
| 733 | |
| 734 | if (STRCMP(name, "add") == 0 && argcount == 2) |
| 735 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 736 | type_T *type = get_type_on_stack(cctx, 1); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 737 | |
| 738 | // add() can be compiled to instructions if we know the type |
| 739 | if (type->tt_type == VAR_LIST) |
| 740 | { |
| 741 | // inline "add(list, item)" so that the type can be checked |
| 742 | res = generate_LISTAPPEND(cctx); |
| 743 | idx = -1; |
| 744 | } |
| 745 | else if (type->tt_type == VAR_BLOB) |
| 746 | { |
| 747 | // inline "add(blob, nr)" so that the type can be checked |
| 748 | res = generate_BLOBAPPEND(cctx); |
| 749 | idx = -1; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | if (idx >= 0) |
| 754 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
| 755 | } |
| 756 | else |
| 757 | semsg(_(e_unknown_function_str), namebuf); |
| 758 | goto theend; |
| 759 | } |
| 760 | |
| 761 | // An argument or local variable can be a function reference, this |
| 762 | // overrules a function name. |
| 763 | if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL |
| 764 | && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK) |
| 765 | { |
| 766 | // If we can find the function by name generate the right call. |
| 767 | // Skip global functions here, a local funcref takes precedence. |
| 768 | ufunc = find_func(name, FALSE, cctx); |
| 769 | if (ufunc != NULL && !func_is_global(ufunc)) |
| 770 | { |
| 771 | res = generate_CALL(cctx, ufunc, argcount); |
| 772 | goto theend; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | // If the name is a variable, load it and use PCALL. |
| 777 | // Not for g:Func(), we don't know if it is a variable or not. |
| 778 | // Not for eome#Func(), it will be loaded later. |
| 779 | p = namebuf; |
| 780 | if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload |
| 781 | && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK) |
| 782 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 783 | type_T *type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 784 | |
| 785 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
| 786 | goto theend; |
| 787 | } |
| 788 | |
| 789 | // If we can find a global function by name generate the right call. |
| 790 | if (ufunc != NULL) |
| 791 | { |
| 792 | res = generate_CALL(cctx, ufunc, argcount); |
| 793 | goto theend; |
| 794 | } |
| 795 | |
| 796 | // A global function may be defined only later. Need to figure out at |
| 797 | // runtime. Also handles a FuncRef at runtime. |
| 798 | if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload) |
| 799 | res = generate_UCALL(cctx, name, argcount); |
| 800 | else |
| 801 | semsg(_(e_unknown_function_str), namebuf); |
| 802 | |
| 803 | theend: |
| 804 | vim_free(tofree); |
| 805 | return res; |
| 806 | } |
| 807 | |
| 808 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 809 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 810 | |
| 811 | /* |
| 812 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 813 | * does not recognize magic braces. |
| 814 | * When "use_namespace" is TRUE recognize "b:", "s:", etc. |
| 815 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 816 | * valid name. |
| 817 | */ |
| 818 | char_u * |
| 819 | to_name_end(char_u *arg, int use_namespace) |
| 820 | { |
| 821 | char_u *p; |
| 822 | |
| 823 | // Quick check for valid starting character. |
| 824 | if (!eval_isnamec1(*arg)) |
| 825 | return arg; |
| 826 | |
| 827 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 828 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 829 | // and can be used in slice "[n:]". |
| 830 | if (*p == ':' && (p != arg + 1 |
| 831 | || !use_namespace |
| 832 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 833 | break; |
| 834 | return p; |
| 835 | } |
| 836 | |
| 837 | /* |
| 838 | * Like to_name_end() but also skip over a list or dict constant. |
| 839 | * Also accept "<SNR>123_Func". |
| 840 | * This intentionally does not handle line continuation. |
| 841 | */ |
| 842 | char_u * |
| 843 | to_name_const_end(char_u *arg) |
| 844 | { |
| 845 | char_u *p = arg; |
| 846 | typval_T rettv; |
| 847 | |
| 848 | if (STRNCMP(p, "<SNR>", 5) == 0) |
| 849 | p = skipdigits(p + 5); |
| 850 | p = to_name_end(p, TRUE); |
| 851 | if (p == arg && *arg == '[') |
| 852 | { |
| 853 | |
| 854 | // Can be "[1, 2, 3]->Func()". |
| 855 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
| 856 | p = arg; |
| 857 | } |
| 858 | return p; |
| 859 | } |
| 860 | |
| 861 | /* |
| 862 | * parse a list: [expr, expr] |
| 863 | * "*arg" points to the '['. |
| 864 | * ppconst->pp_is_const is set if all items are a constant. |
| 865 | */ |
| 866 | static int |
| 867 | compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 868 | { |
| 869 | char_u *p = skipwhite(*arg + 1); |
| 870 | char_u *whitep = *arg + 1; |
| 871 | int count = 0; |
| 872 | int is_const; |
| 873 | int is_all_const = TRUE; // reset when non-const encountered |
| 874 | |
| 875 | for (;;) |
| 876 | { |
| 877 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 878 | { |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 879 | semsg(_(e_missing_end_of_list_rsb_str), *arg); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 880 | return FAIL; |
| 881 | } |
| 882 | if (*p == ',') |
| 883 | { |
| 884 | semsg(_(e_no_white_space_allowed_before_str_str), ",", p); |
| 885 | return FAIL; |
| 886 | } |
| 887 | if (*p == ']') |
| 888 | { |
| 889 | ++p; |
| 890 | break; |
| 891 | } |
| 892 | if (compile_expr0_ext(&p, cctx, &is_const) == FAIL) |
| 893 | return FAIL; |
| 894 | if (!is_const) |
| 895 | is_all_const = FALSE; |
| 896 | ++count; |
| 897 | if (*p == ',') |
| 898 | { |
| 899 | ++p; |
| 900 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 901 | { |
| 902 | semsg(_(e_white_space_required_after_str_str), ",", p - 1); |
| 903 | return FAIL; |
| 904 | } |
| 905 | } |
| 906 | whitep = p; |
| 907 | p = skipwhite(p); |
| 908 | } |
| 909 | *arg = p; |
| 910 | |
| 911 | ppconst->pp_is_const = is_all_const; |
| 912 | return generate_NEWLIST(cctx, count); |
| 913 | } |
| 914 | |
| 915 | /* |
| 916 | * Parse a lambda: "(arg, arg) => expr" |
| 917 | * "*arg" points to the '('. |
| 918 | * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda. |
| 919 | */ |
| 920 | static int |
| 921 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 922 | { |
| 923 | int r; |
| 924 | typval_T rettv; |
| 925 | ufunc_T *ufunc; |
| 926 | evalarg_T evalarg; |
| 927 | |
| 928 | init_evalarg(&evalarg); |
| 929 | evalarg.eval_flags = EVAL_EVALUATE; |
| 930 | evalarg.eval_cctx = cctx; |
| 931 | |
| 932 | // Get the funcref in "rettv". |
| 933 | r = get_lambda_tv(arg, &rettv, TRUE, &evalarg); |
| 934 | if (r != OK) |
| 935 | { |
| 936 | clear_evalarg(&evalarg, NULL); |
| 937 | return r; |
| 938 | } |
| 939 | |
| 940 | // "rettv" will now be a partial referencing the function. |
| 941 | ufunc = rettv.vval.v_partial->pt_func; |
| 942 | ++ufunc->uf_refcount; |
| 943 | clear_tv(&rettv); |
| 944 | |
| 945 | // Compile it here to get the return type. The return type is optional, |
| 946 | // when it's missing use t_unknown. This is recognized in |
| 947 | // compile_return(). |
| 948 | if (ufunc->uf_ret_type->tt_type == VAR_VOID) |
| 949 | ufunc->uf_ret_type = &t_unknown; |
| 950 | compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx); |
| 951 | |
| 952 | // When the outer function is compiled for profiling or debugging, the |
| 953 | // lambda may be called without profiling or debugging. Compile it here in |
| 954 | // the right context. |
| 955 | if (cctx->ctx_compile_type == CT_DEBUG |
| 956 | #ifdef FEAT_PROFILE |
| 957 | || cctx->ctx_compile_type == CT_PROFILE |
| 958 | #endif |
| 959 | ) |
| 960 | compile_def_function(ufunc, FALSE, CT_NONE, cctx); |
| 961 | |
| 962 | // The last entry in evalarg.eval_tofree_ga is a copy of the last line and |
| 963 | // "*arg" may point into it. Point into the original line to avoid a |
| 964 | // dangling pointer. |
| 965 | if (evalarg.eval_using_cmdline) |
| 966 | { |
| 967 | garray_T *gap = &evalarg.eval_tofree_ga; |
| 968 | size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1]; |
| 969 | |
| 970 | *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum] |
| 971 | + off; |
| 972 | } |
| 973 | |
| 974 | clear_evalarg(&evalarg, NULL); |
| 975 | |
| 976 | if (ufunc->uf_def_status == UF_COMPILED) |
| 977 | { |
| 978 | // The return type will now be known. |
| 979 | set_function_type(ufunc); |
| 980 | |
| 981 | // The function reference count will be 1. When the ISN_FUNCREF |
| 982 | // instruction is deleted the reference count is decremented and the |
| 983 | // function is freed. |
| 984 | return generate_FUNCREF(cctx, ufunc); |
| 985 | } |
| 986 | |
| 987 | func_ptr_unref(ufunc); |
| 988 | return FAIL; |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Get a lambda and compile it. Uses Vim9 syntax. |
| 993 | */ |
| 994 | int |
| 995 | get_lambda_tv_and_compile( |
| 996 | char_u **arg, |
| 997 | typval_T *rettv, |
| 998 | int types_optional, |
| 999 | evalarg_T *evalarg) |
| 1000 | { |
| 1001 | int r; |
| 1002 | ufunc_T *ufunc; |
| 1003 | int save_sc_version = current_sctx.sc_version; |
| 1004 | |
| 1005 | // Get the funcref in "rettv". |
| 1006 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 1007 | r = get_lambda_tv(arg, rettv, types_optional, evalarg); |
| 1008 | current_sctx.sc_version = save_sc_version; |
| 1009 | if (r != OK) |
| 1010 | return r; |
| 1011 | |
| 1012 | // "rettv" will now be a partial referencing the function. |
| 1013 | ufunc = rettv->vval.v_partial->pt_func; |
| 1014 | |
| 1015 | // Compile it here to get the return type. The return type is optional, |
| 1016 | // when it's missing use t_unknown. This is recognized in |
| 1017 | // compile_return(). |
| 1018 | if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID) |
| 1019 | ufunc->uf_ret_type = &t_unknown; |
| 1020 | compile_def_function(ufunc, FALSE, CT_NONE, NULL); |
| 1021 | |
| 1022 | if (ufunc->uf_def_status == UF_COMPILED) |
| 1023 | { |
| 1024 | // The return type will now be known. |
| 1025 | set_function_type(ufunc); |
| 1026 | return OK; |
| 1027 | } |
| 1028 | clear_tv(rettv); |
| 1029 | return FAIL; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * parse a dict: {key: val, [key]: val} |
| 1034 | * "*arg" points to the '{'. |
| 1035 | * ppconst->pp_is_const is set if all item values are a constant. |
| 1036 | */ |
| 1037 | static int |
| 1038 | compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 1039 | { |
| 1040 | garray_T *instr = &cctx->ctx_instr; |
| 1041 | int count = 0; |
| 1042 | dict_T *d = dict_alloc(); |
| 1043 | dictitem_T *item; |
| 1044 | char_u *whitep = *arg + 1; |
| 1045 | char_u *p; |
| 1046 | int is_const; |
| 1047 | int is_all_const = TRUE; // reset when non-const encountered |
| 1048 | |
| 1049 | if (d == NULL) |
| 1050 | return FAIL; |
| 1051 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 1052 | return FAIL; |
| 1053 | for (;;) |
| 1054 | { |
| 1055 | char_u *key = NULL; |
| 1056 | |
| 1057 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 1058 | { |
| 1059 | *arg = NULL; |
| 1060 | goto failret; |
| 1061 | } |
| 1062 | |
| 1063 | if (**arg == '}') |
| 1064 | break; |
| 1065 | |
| 1066 | if (**arg == '[') |
| 1067 | { |
| 1068 | isn_T *isn; |
| 1069 | |
| 1070 | // {[expr]: value} uses an evaluated key. |
| 1071 | *arg = skipwhite(*arg + 1); |
| 1072 | if (compile_expr0(arg, cctx) == FAIL) |
| 1073 | return FAIL; |
| 1074 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 1075 | if (isn->isn_type == ISN_PUSHNR) |
| 1076 | { |
| 1077 | char buf[NUMBUFLEN]; |
| 1078 | |
| 1079 | // Convert to string at compile time. |
| 1080 | vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number); |
| 1081 | isn->isn_type = ISN_PUSHS; |
| 1082 | isn->isn_arg.string = vim_strsave((char_u *)buf); |
| 1083 | } |
| 1084 | if (isn->isn_type == ISN_PUSHS) |
| 1085 | key = isn->isn_arg.string; |
| 1086 | else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL) |
| 1087 | return FAIL; |
| 1088 | *arg = skipwhite(*arg); |
| 1089 | if (**arg != ']') |
| 1090 | { |
| 1091 | emsg(_(e_missing_matching_bracket_after_dict_key)); |
| 1092 | return FAIL; |
| 1093 | } |
| 1094 | ++*arg; |
| 1095 | } |
| 1096 | else |
| 1097 | { |
| 1098 | // {"name": value}, |
| 1099 | // {'name': value}, |
| 1100 | // {name: value} use "name" as a literal key |
| 1101 | key = get_literal_key(arg); |
| 1102 | if (key == NULL) |
| 1103 | return FAIL; |
| 1104 | if (generate_PUSHS(cctx, &key) == FAIL) |
| 1105 | return FAIL; |
| 1106 | } |
| 1107 | |
| 1108 | // Check for duplicate keys, if using string keys. |
| 1109 | if (key != NULL) |
| 1110 | { |
| 1111 | item = dict_find(d, key, -1); |
| 1112 | if (item != NULL) |
| 1113 | { |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 1114 | semsg(_(e_duplicate_key_in_dicitonary), key); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1115 | goto failret; |
| 1116 | } |
| 1117 | item = dictitem_alloc(key); |
| 1118 | if (item != NULL) |
| 1119 | { |
| 1120 | item->di_tv.v_type = VAR_UNKNOWN; |
| 1121 | item->di_tv.v_lock = 0; |
| 1122 | if (dict_add(d, item) == FAIL) |
| 1123 | dictitem_free(item); |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | if (**arg != ':') |
| 1128 | { |
| 1129 | if (*skipwhite(*arg) == ':') |
| 1130 | semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg); |
| 1131 | else |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 1132 | semsg(_(e_missing_colon_in_dictionary), *arg); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1133 | return FAIL; |
| 1134 | } |
| 1135 | whitep = *arg + 1; |
| 1136 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 1137 | { |
| 1138 | semsg(_(e_white_space_required_after_str_str), ":", *arg); |
| 1139 | return FAIL; |
| 1140 | } |
| 1141 | |
| 1142 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 1143 | { |
| 1144 | *arg = NULL; |
| 1145 | goto failret; |
| 1146 | } |
| 1147 | |
| 1148 | if (compile_expr0_ext(arg, cctx, &is_const) == FAIL) |
| 1149 | return FAIL; |
| 1150 | if (!is_const) |
| 1151 | is_all_const = FALSE; |
| 1152 | ++count; |
| 1153 | |
| 1154 | whitep = *arg; |
| 1155 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 1156 | { |
| 1157 | *arg = NULL; |
| 1158 | goto failret; |
| 1159 | } |
| 1160 | if (**arg == '}') |
| 1161 | break; |
| 1162 | if (**arg != ',') |
| 1163 | { |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 1164 | semsg(_(e_missing_comma_in_dictionary), *arg); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1165 | goto failret; |
| 1166 | } |
| 1167 | if (IS_WHITE_OR_NUL(*whitep)) |
| 1168 | { |
| 1169 | semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep); |
| 1170 | return FAIL; |
| 1171 | } |
| 1172 | whitep = *arg + 1; |
| 1173 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 1174 | { |
| 1175 | semsg(_(e_white_space_required_after_str_str), ",", *arg); |
| 1176 | return FAIL; |
| 1177 | } |
| 1178 | *arg = skipwhite(whitep); |
| 1179 | } |
| 1180 | |
| 1181 | *arg = *arg + 1; |
| 1182 | |
| 1183 | // Allow for following comment, after at least one space. |
| 1184 | p = skipwhite(*arg); |
| 1185 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
| 1186 | *arg += STRLEN(*arg); |
| 1187 | |
| 1188 | dict_unref(d); |
| 1189 | ppconst->pp_is_const = is_all_const; |
| 1190 | return generate_NEWDICT(cctx, count); |
| 1191 | |
| 1192 | failret: |
| 1193 | if (*arg == NULL) |
| 1194 | { |
| 1195 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
| 1196 | *arg = (char_u *)""; |
| 1197 | } |
| 1198 | dict_unref(d); |
| 1199 | return FAIL; |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | * Compile "&option". |
| 1204 | */ |
| 1205 | static int |
| 1206 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 1207 | { |
| 1208 | typval_T rettv; |
| 1209 | char_u *start = *arg; |
| 1210 | int ret; |
| 1211 | |
| 1212 | // parse the option and get the current value to get the type. |
| 1213 | rettv.v_type = VAR_UNKNOWN; |
| 1214 | ret = eval_option(arg, &rettv, TRUE); |
| 1215 | if (ret == OK) |
| 1216 | { |
| 1217 | // include the '&' in the name, eval_option() expects it. |
| 1218 | char_u *name = vim_strnsave(start, *arg - start); |
| 1219 | type_T *type = rettv.v_type == VAR_BOOL ? &t_bool |
| 1220 | : rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 1221 | |
| 1222 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 1223 | vim_free(name); |
| 1224 | } |
| 1225 | clear_tv(&rettv); |
| 1226 | |
| 1227 | return ret; |
| 1228 | } |
| 1229 | |
| 1230 | /* |
| 1231 | * Compile "$VAR". |
| 1232 | */ |
| 1233 | static int |
| 1234 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 1235 | { |
| 1236 | char_u *start = *arg; |
| 1237 | int len; |
| 1238 | int ret; |
| 1239 | char_u *name; |
| 1240 | |
| 1241 | ++*arg; |
| 1242 | len = get_env_len(arg); |
| 1243 | if (len == 0) |
| 1244 | { |
| 1245 | semsg(_(e_syntax_error_at_str), start - 1); |
| 1246 | return FAIL; |
| 1247 | } |
| 1248 | |
| 1249 | // include the '$' in the name, eval_env_var() expects it. |
| 1250 | name = vim_strnsave(start, len + 1); |
| 1251 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 1252 | vim_free(name); |
| 1253 | return ret; |
| 1254 | } |
| 1255 | |
| 1256 | /* |
| 1257 | * Compile "@r". |
| 1258 | */ |
| 1259 | static int |
| 1260 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 1261 | { |
| 1262 | int ret; |
| 1263 | |
| 1264 | ++*arg; |
| 1265 | if (**arg == NUL) |
| 1266 | { |
| 1267 | semsg(_(e_syntax_error_at_str), *arg - 1); |
| 1268 | return FAIL; |
| 1269 | } |
| 1270 | if (!valid_yank_reg(**arg, FALSE)) |
| 1271 | { |
| 1272 | emsg_invreg(**arg); |
| 1273 | return FAIL; |
| 1274 | } |
| 1275 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 1276 | ++*arg; |
| 1277 | return ret; |
| 1278 | } |
| 1279 | |
| 1280 | /* |
| 1281 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 1282 | * When "numeric_only" is TRUE do not apply '!'. |
| 1283 | */ |
| 1284 | static int |
| 1285 | apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end) |
| 1286 | { |
| 1287 | char_u *p = *end; |
| 1288 | |
| 1289 | // this works from end to start |
| 1290 | while (p > start) |
| 1291 | { |
| 1292 | --p; |
| 1293 | if (*p == '-' || *p == '+') |
| 1294 | { |
| 1295 | // only '-' has an effect, for '+' we only check the type |
| 1296 | #ifdef FEAT_FLOAT |
| 1297 | if (rettv->v_type == VAR_FLOAT) |
| 1298 | { |
| 1299 | if (*p == '-') |
| 1300 | rettv->vval.v_float = -rettv->vval.v_float; |
| 1301 | } |
| 1302 | else |
| 1303 | #endif |
| 1304 | { |
| 1305 | varnumber_T val; |
| 1306 | int error = FALSE; |
| 1307 | |
| 1308 | // tv_get_number_chk() accepts a string, but we don't want that |
| 1309 | // here |
| 1310 | if (check_not_string(rettv) == FAIL) |
| 1311 | return FAIL; |
| 1312 | val = tv_get_number_chk(rettv, &error); |
| 1313 | clear_tv(rettv); |
| 1314 | if (error) |
| 1315 | return FAIL; |
| 1316 | if (*p == '-') |
| 1317 | val = -val; |
| 1318 | rettv->v_type = VAR_NUMBER; |
| 1319 | rettv->vval.v_number = val; |
| 1320 | } |
| 1321 | } |
| 1322 | else if (numeric_only) |
| 1323 | { |
| 1324 | ++p; |
| 1325 | break; |
| 1326 | } |
| 1327 | else if (*p == '!') |
| 1328 | { |
| 1329 | int v = tv2bool(rettv); |
| 1330 | |
| 1331 | // '!' is permissive in the type. |
| 1332 | clear_tv(rettv); |
| 1333 | rettv->v_type = VAR_BOOL; |
| 1334 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 1335 | } |
| 1336 | } |
| 1337 | *end = p; |
| 1338 | return OK; |
| 1339 | } |
| 1340 | |
| 1341 | /* |
| 1342 | * Recognize v: variables that are constants and set "rettv". |
| 1343 | */ |
| 1344 | static void |
| 1345 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 1346 | { |
| 1347 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 1348 | { |
| 1349 | rettv->v_type = VAR_BOOL; |
| 1350 | rettv->vval.v_number = VVAL_TRUE; |
| 1351 | *arg += 6; |
| 1352 | } |
| 1353 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 1354 | { |
| 1355 | rettv->v_type = VAR_BOOL; |
| 1356 | rettv->vval.v_number = VVAL_FALSE; |
| 1357 | *arg += 7; |
| 1358 | } |
| 1359 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 1360 | { |
| 1361 | rettv->v_type = VAR_SPECIAL; |
| 1362 | rettv->vval.v_number = VVAL_NULL; |
| 1363 | *arg += 6; |
| 1364 | } |
| 1365 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 1366 | { |
| 1367 | rettv->v_type = VAR_SPECIAL; |
| 1368 | rettv->vval.v_number = VVAL_NONE; |
| 1369 | *arg += 6; |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | exprtype_T |
| 1374 | get_compare_type(char_u *p, int *len, int *type_is) |
| 1375 | { |
| 1376 | exprtype_T type = EXPR_UNKNOWN; |
| 1377 | int i; |
| 1378 | |
| 1379 | switch (p[0]) |
| 1380 | { |
| 1381 | case '=': if (p[1] == '=') |
| 1382 | type = EXPR_EQUAL; |
| 1383 | else if (p[1] == '~') |
| 1384 | type = EXPR_MATCH; |
| 1385 | break; |
| 1386 | case '!': if (p[1] == '=') |
| 1387 | type = EXPR_NEQUAL; |
| 1388 | else if (p[1] == '~') |
| 1389 | type = EXPR_NOMATCH; |
| 1390 | break; |
| 1391 | case '>': if (p[1] != '=') |
| 1392 | { |
| 1393 | type = EXPR_GREATER; |
| 1394 | *len = 1; |
| 1395 | } |
| 1396 | else |
| 1397 | type = EXPR_GEQUAL; |
| 1398 | break; |
| 1399 | case '<': if (p[1] != '=') |
| 1400 | { |
| 1401 | type = EXPR_SMALLER; |
| 1402 | *len = 1; |
| 1403 | } |
| 1404 | else |
| 1405 | type = EXPR_SEQUAL; |
| 1406 | break; |
| 1407 | case 'i': if (p[1] == 's') |
| 1408 | { |
| 1409 | // "is" and "isnot"; but not a prefix of a name |
| 1410 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 1411 | *len = 5; |
| 1412 | i = p[*len]; |
| 1413 | if (!isalnum(i) && i != '_') |
| 1414 | { |
| 1415 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 1416 | *type_is = TRUE; |
| 1417 | } |
| 1418 | } |
| 1419 | break; |
| 1420 | } |
| 1421 | return type; |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * Skip over an expression, ignoring most errors. |
| 1426 | */ |
| 1427 | void |
| 1428 | skip_expr_cctx(char_u **arg, cctx_T *cctx) |
| 1429 | { |
| 1430 | evalarg_T evalarg; |
| 1431 | |
| 1432 | init_evalarg(&evalarg); |
| 1433 | evalarg.eval_cctx = cctx; |
| 1434 | skip_expr(arg, &evalarg); |
| 1435 | clear_evalarg(&evalarg, NULL); |
| 1436 | } |
| 1437 | |
| 1438 | /* |
| 1439 | * Check that the top of the type stack has a type that can be used as a |
| 1440 | * condition. Give an error and return FAIL if not. |
| 1441 | */ |
| 1442 | int |
| 1443 | bool_on_stack(cctx_T *cctx) |
| 1444 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1445 | type_T *type; |
| 1446 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1447 | type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1448 | if (type == &t_bool) |
| 1449 | return OK; |
| 1450 | |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 1451 | if (type == &t_any |
| 1452 | || type == &t_unknown |
| 1453 | || type == &t_number |
| 1454 | || type == &t_number_bool) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1455 | // Number 0 and 1 are OK to use as a bool. "any" could also be a bool. |
| 1456 | // This requires a runtime type check. |
| 1457 | return generate_COND2BOOL(cctx); |
| 1458 | |
| 1459 | return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE); |
| 1460 | } |
| 1461 | |
| 1462 | /* |
| 1463 | * Give the "white on both sides" error, taking the operator from "p[len]". |
| 1464 | */ |
| 1465 | void |
| 1466 | error_white_both(char_u *op, int len) |
| 1467 | { |
| 1468 | char_u buf[10]; |
| 1469 | |
| 1470 | vim_strncpy(buf, op, len); |
| 1471 | semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op); |
| 1472 | } |
| 1473 | |
| 1474 | /* |
| 1475 | * Compile code to apply '-', '+' and '!'. |
| 1476 | * When "numeric_only" is TRUE do not apply '!'. |
| 1477 | */ |
| 1478 | static int |
| 1479 | compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end) |
| 1480 | { |
| 1481 | char_u *p = *end; |
| 1482 | |
| 1483 | // this works from end to start |
| 1484 | while (p > start) |
| 1485 | { |
| 1486 | --p; |
| 1487 | while (VIM_ISWHITE(*p)) |
| 1488 | --p; |
| 1489 | if (*p == '-' || *p == '+') |
| 1490 | { |
| 1491 | int negate = *p == '-'; |
| 1492 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1493 | type_T *type; |
| 1494 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1495 | type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1496 | if (type != &t_float && need_type(type, &t_number, |
| 1497 | -1, 0, cctx, FALSE, FALSE) == FAIL) |
| 1498 | return FAIL; |
| 1499 | |
| 1500 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 1501 | { |
| 1502 | --p; |
| 1503 | if (*p == '-') |
| 1504 | negate = !negate; |
| 1505 | } |
| 1506 | // only '-' has an effect, for '+' we only check the type |
| 1507 | if (negate) |
| 1508 | { |
| 1509 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 1510 | if (isn == NULL) |
| 1511 | return FAIL; |
| 1512 | } |
| 1513 | } |
| 1514 | else if (numeric_only) |
| 1515 | { |
| 1516 | ++p; |
| 1517 | break; |
| 1518 | } |
| 1519 | else |
| 1520 | { |
| 1521 | int invert = *p == '!'; |
| 1522 | |
| 1523 | while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1]))) |
| 1524 | { |
| 1525 | if (p[-1] == '!') |
| 1526 | invert = !invert; |
| 1527 | --p; |
| 1528 | } |
| 1529 | if (generate_2BOOL(cctx, invert, -1) == FAIL) |
| 1530 | return FAIL; |
| 1531 | } |
| 1532 | } |
| 1533 | *end = p; |
| 1534 | return OK; |
| 1535 | } |
| 1536 | |
| 1537 | /* |
| 1538 | * Compile "(expression)": recursive! |
| 1539 | * Return FAIL/OK. |
| 1540 | */ |
| 1541 | static int |
| 1542 | compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 1543 | { |
| 1544 | int ret; |
| 1545 | char_u *p = *arg + 1; |
| 1546 | |
| 1547 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 1548 | return FAIL; |
| 1549 | if (ppconst->pp_used <= PPSIZE - 10) |
| 1550 | { |
| 1551 | ret = compile_expr1(arg, cctx, ppconst); |
| 1552 | } |
| 1553 | else |
| 1554 | { |
| 1555 | // Not enough space in ppconst, flush constants. |
| 1556 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 1557 | return FAIL; |
| 1558 | ret = compile_expr0(arg, cctx); |
| 1559 | } |
| 1560 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
| 1561 | return FAIL; |
| 1562 | if (**arg == ')') |
| 1563 | ++*arg; |
| 1564 | else if (ret == OK) |
| 1565 | { |
| 1566 | emsg(_(e_missing_closing_paren)); |
| 1567 | ret = FAIL; |
| 1568 | } |
| 1569 | return ret; |
| 1570 | } |
| 1571 | |
| 1572 | /* |
| 1573 | * Compile whatever comes after "name" or "name()". |
| 1574 | * Advances "*arg" only when something was recognized. |
| 1575 | */ |
| 1576 | static int |
| 1577 | compile_subscript( |
| 1578 | char_u **arg, |
| 1579 | cctx_T *cctx, |
| 1580 | char_u *start_leader, |
| 1581 | char_u **end_leader, |
| 1582 | ppconst_T *ppconst) |
| 1583 | { |
| 1584 | char_u *name_start = *end_leader; |
| 1585 | int keeping_dict = FALSE; |
| 1586 | |
| 1587 | for (;;) |
| 1588 | { |
| 1589 | char_u *p = skipwhite(*arg); |
| 1590 | |
| 1591 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
| 1592 | { |
| 1593 | char_u *next = peek_next_line_from_context(cctx); |
| 1594 | |
| 1595 | // If a following line starts with "->{" or "->X" advance to that |
| 1596 | // line, so that a line break before "->" is allowed. |
| 1597 | // Also if a following line starts with ".x". |
| 1598 | if (next != NULL && |
| 1599 | ((next[0] == '-' && next[1] == '>' |
| 1600 | && (next[2] == '{' |
| 1601 | || ASCII_ISALPHA(*skipwhite(next + 2)))) |
| 1602 | || (next[0] == '.' && eval_isdictc(next[1])))) |
| 1603 | { |
| 1604 | next = next_line_from_context(cctx, TRUE); |
| 1605 | if (next == NULL) |
| 1606 | return FAIL; |
| 1607 | *arg = next; |
| 1608 | p = skipwhite(*arg); |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | // Do not skip over white space to find the "(", "execute 'x' (expr)" |
| 1613 | // is not a function call. |
| 1614 | if (**arg == '(') |
| 1615 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1616 | type_T *type; |
| 1617 | int argcount = 0; |
| 1618 | |
| 1619 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 1620 | return FAIL; |
| 1621 | ppconst->pp_is_const = FALSE; |
| 1622 | |
| 1623 | // funcref(arg) |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1624 | type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1625 | |
| 1626 | *arg = skipwhite(p + 1); |
| 1627 | if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL) |
| 1628 | return FAIL; |
| 1629 | if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL) |
| 1630 | return FAIL; |
| 1631 | if (keeping_dict) |
| 1632 | { |
| 1633 | keeping_dict = FALSE; |
| 1634 | if (generate_instr(cctx, ISN_CLEARDICT) == NULL) |
| 1635 | return FAIL; |
| 1636 | } |
| 1637 | } |
| 1638 | else if (*p == '-' && p[1] == '>') |
| 1639 | { |
| 1640 | char_u *pstart = p; |
| 1641 | |
| 1642 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 1643 | return FAIL; |
| 1644 | ppconst->pp_is_const = FALSE; |
| 1645 | |
| 1646 | // something->method() |
| 1647 | // Apply the '!', '-' and '+' first: |
| 1648 | // -1.0->func() works like (-1.0)->func() |
| 1649 | if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL) |
| 1650 | return FAIL; |
| 1651 | |
| 1652 | p += 2; |
| 1653 | *arg = skipwhite(p); |
| 1654 | // No line break supported right after "->". |
| 1655 | if (**arg == '(') |
| 1656 | { |
| 1657 | int argcount = 1; |
| 1658 | garray_T *stack = &cctx->ctx_type_stack; |
| 1659 | int type_idx_start = stack->ga_len; |
| 1660 | type_T *type; |
| 1661 | int expr_isn_start = cctx->ctx_instr.ga_len; |
| 1662 | int expr_isn_end; |
| 1663 | int arg_isn_count; |
| 1664 | |
| 1665 | // Funcref call: list->(Refs[2])(arg) |
| 1666 | // or lambda: list->((arg) => expr)(arg) |
| 1667 | // |
| 1668 | // Fist compile the function expression. |
| 1669 | if (compile_parenthesis(arg, cctx, ppconst) == FAIL) |
| 1670 | return FAIL; |
| 1671 | |
| 1672 | // Remember the next instruction index, where the instructions |
| 1673 | // for arguments are being written. |
| 1674 | expr_isn_end = cctx->ctx_instr.ga_len; |
| 1675 | |
| 1676 | // Compile the arguments. |
| 1677 | if (**arg != '(') |
| 1678 | { |
| 1679 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | 3a846e6 | 2022-01-01 16:21:00 +0000 | [diff] [blame] | 1680 | emsg(_(e_no_white_space_allowed_before_parenthesis)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1681 | else |
| 1682 | semsg(_(e_missing_parenthesis_str), *arg); |
| 1683 | return FAIL; |
| 1684 | } |
| 1685 | *arg = skipwhite(*arg + 1); |
| 1686 | if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL) |
| 1687 | return FAIL; |
| 1688 | |
| 1689 | // Move the instructions for the arguments to before the |
| 1690 | // instructions of the expression and move the type of the |
| 1691 | // expression after the argument types. This is what ISN_PCALL |
| 1692 | // expects. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1693 | arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end; |
| 1694 | if (arg_isn_count > 0) |
| 1695 | { |
| 1696 | int expr_isn_count = expr_isn_end - expr_isn_start; |
| 1697 | isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1698 | type_T *decl_type; |
| 1699 | type2_T *typep; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1700 | |
| 1701 | if (isn == NULL) |
| 1702 | return FAIL; |
| 1703 | mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data) |
| 1704 | + expr_isn_start, |
| 1705 | sizeof(isn_T) * expr_isn_count); |
| 1706 | mch_memmove(((isn_T *)cctx->ctx_instr.ga_data) |
| 1707 | + expr_isn_start, |
| 1708 | ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end, |
| 1709 | sizeof(isn_T) * arg_isn_count); |
| 1710 | mch_memmove(((isn_T *)cctx->ctx_instr.ga_data) |
| 1711 | + expr_isn_start + arg_isn_count, |
| 1712 | isn, sizeof(isn_T) * expr_isn_count); |
| 1713 | vim_free(isn); |
| 1714 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1715 | typep = ((type2_T *)stack->ga_data) + type_idx_start; |
| 1716 | type = typep->type_curr; |
| 1717 | decl_type = typep->type_decl; |
| 1718 | mch_memmove(((type2_T *)stack->ga_data) + type_idx_start, |
| 1719 | ((type2_T *)stack->ga_data) + type_idx_start + 1, |
| 1720 | sizeof(type2_T) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1721 | * (stack->ga_len - type_idx_start - 1)); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1722 | typep = ((type2_T *)stack->ga_data) + stack->ga_len - 1; |
| 1723 | typep->type_curr = type; |
| 1724 | typep->type_decl = decl_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1727 | type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1728 | if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL) |
| 1729 | return FAIL; |
| 1730 | } |
| 1731 | else |
| 1732 | { |
| 1733 | // method call: list->method() |
| 1734 | p = *arg; |
| 1735 | if (!eval_isnamec1(*p)) |
| 1736 | { |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 1737 | semsg(_(e_trailing_characters_str), pstart); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1738 | return FAIL; |
| 1739 | } |
| 1740 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 1741 | p += 2; |
| 1742 | for ( ; eval_isnamec(*p); ++p) |
| 1743 | ; |
| 1744 | if (*p != '(') |
| 1745 | { |
| 1746 | semsg(_(e_missing_parenthesis_str), *arg); |
| 1747 | return FAIL; |
| 1748 | } |
| 1749 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
| 1750 | return FAIL; |
| 1751 | } |
| 1752 | if (keeping_dict) |
| 1753 | { |
| 1754 | keeping_dict = FALSE; |
| 1755 | if (generate_instr(cctx, ISN_CLEARDICT) == NULL) |
| 1756 | return FAIL; |
| 1757 | } |
| 1758 | } |
| 1759 | else if (**arg == '[') |
| 1760 | { |
| 1761 | int is_slice = FALSE; |
| 1762 | |
| 1763 | // list index: list[123] |
| 1764 | // dict member: dict[key] |
| 1765 | // string index: text[123] |
| 1766 | // blob index: blob[123] |
| 1767 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 1768 | return FAIL; |
| 1769 | ppconst->pp_is_const = FALSE; |
| 1770 | |
| 1771 | ++p; |
| 1772 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 1773 | return FAIL; |
| 1774 | if (**arg == ':') |
| 1775 | { |
| 1776 | // missing first index is equal to zero |
| 1777 | generate_PUSHNR(cctx, 0); |
| 1778 | } |
| 1779 | else |
| 1780 | { |
| 1781 | if (compile_expr0(arg, cctx) == FAIL) |
| 1782 | return FAIL; |
| 1783 | if (**arg == ':') |
| 1784 | { |
| 1785 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 1786 | ":", *arg); |
| 1787 | return FAIL; |
| 1788 | } |
| 1789 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
| 1790 | return FAIL; |
| 1791 | *arg = skipwhite(*arg); |
| 1792 | } |
| 1793 | if (**arg == ':') |
| 1794 | { |
| 1795 | is_slice = TRUE; |
| 1796 | ++*arg; |
| 1797 | if (!IS_WHITE_OR_NUL(**arg) && **arg != ']') |
| 1798 | { |
| 1799 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 1800 | ":", *arg); |
| 1801 | return FAIL; |
| 1802 | } |
| 1803 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
| 1804 | return FAIL; |
| 1805 | if (**arg == ']') |
| 1806 | // missing second index is equal to end of string |
| 1807 | generate_PUSHNR(cctx, -1); |
| 1808 | else |
| 1809 | { |
| 1810 | if (compile_expr0(arg, cctx) == FAIL) |
| 1811 | return FAIL; |
| 1812 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
| 1813 | return FAIL; |
| 1814 | *arg = skipwhite(*arg); |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | if (**arg != ']') |
| 1819 | { |
| 1820 | emsg(_(e_missing_closing_square_brace)); |
| 1821 | return FAIL; |
| 1822 | } |
| 1823 | *arg = *arg + 1; |
| 1824 | |
| 1825 | if (keeping_dict) |
| 1826 | { |
| 1827 | keeping_dict = FALSE; |
| 1828 | if (generate_instr(cctx, ISN_CLEARDICT) == NULL) |
| 1829 | return FAIL; |
| 1830 | } |
| 1831 | if (compile_member(is_slice, &keeping_dict, cctx) == FAIL) |
| 1832 | return FAIL; |
| 1833 | } |
| 1834 | else if (*p == '.' && p[1] != '.') |
| 1835 | { |
| 1836 | // dictionary member: dict.name |
| 1837 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 1838 | return FAIL; |
| 1839 | ppconst->pp_is_const = FALSE; |
| 1840 | |
| 1841 | *arg = p + 1; |
| 1842 | if (IS_WHITE_OR_NUL(**arg)) |
| 1843 | { |
| 1844 | emsg(_(e_missing_name_after_dot)); |
| 1845 | return FAIL; |
| 1846 | } |
| 1847 | p = *arg; |
| 1848 | if (eval_isdictc(*p)) |
| 1849 | while (eval_isnamec(*p)) |
| 1850 | MB_PTR_ADV(p); |
| 1851 | if (p == *arg) |
| 1852 | { |
| 1853 | semsg(_(e_syntax_error_at_str), *arg); |
| 1854 | return FAIL; |
| 1855 | } |
| 1856 | if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL) |
| 1857 | return FAIL; |
| 1858 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
| 1859 | return FAIL; |
| 1860 | keeping_dict = TRUE; |
| 1861 | *arg = p; |
| 1862 | } |
| 1863 | else |
| 1864 | break; |
| 1865 | } |
| 1866 | |
| 1867 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 1868 | // This needs to be done at runtime to be able to check the type. |
| 1869 | if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL) |
| 1870 | return FAIL; |
| 1871 | |
| 1872 | return OK; |
| 1873 | } |
| 1874 | |
| 1875 | /* |
| 1876 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 1877 | * "arg" is advanced until after the expression, skipping white space. |
| 1878 | * |
| 1879 | * If the value is a constant "ppconst->pp_used" will be non-zero. |
| 1880 | * Before instructions are generated, any values in "ppconst" will generated. |
| 1881 | * |
| 1882 | * This is the compiling equivalent of eval1(), eval2(), etc. |
| 1883 | */ |
| 1884 | |
| 1885 | /* |
| 1886 | * number number constant |
| 1887 | * 0zFFFFFFFF Blob constant |
| 1888 | * "string" string constant |
| 1889 | * 'string' literal string constant |
| 1890 | * &option-name option value |
| 1891 | * @r register contents |
| 1892 | * identifier variable value |
| 1893 | * function() function call |
| 1894 | * $VAR environment variable |
| 1895 | * (expression) nested expression |
| 1896 | * [expr, expr] List |
| 1897 | * {key: val, [key]: val} Dictionary |
| 1898 | * |
| 1899 | * Also handle: |
| 1900 | * ! in front logical NOT |
| 1901 | * - in front unary minus |
| 1902 | * + in front unary plus (ignored) |
| 1903 | * trailing (arg) funcref/partial call |
| 1904 | * trailing [] subscript in String or List |
| 1905 | * trailing .name entry in Dictionary |
| 1906 | * trailing ->name() method call |
| 1907 | */ |
| 1908 | static int |
Bram Moolenaar | 5da3605 | 2021-12-27 15:39:57 +0000 | [diff] [blame] | 1909 | compile_expr8( |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1910 | char_u **arg, |
| 1911 | cctx_T *cctx, |
| 1912 | ppconst_T *ppconst) |
| 1913 | { |
| 1914 | char_u *start_leader, *end_leader; |
| 1915 | int ret = OK; |
| 1916 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
| 1917 | int used_before = ppconst->pp_used; |
| 1918 | |
| 1919 | ppconst->pp_is_const = FALSE; |
| 1920 | |
| 1921 | /* |
| 1922 | * Skip '!', '-' and '+' characters. They are handled later. |
| 1923 | */ |
| 1924 | start_leader = *arg; |
| 1925 | if (eval_leader(arg, TRUE) == FAIL) |
| 1926 | return FAIL; |
| 1927 | end_leader = *arg; |
| 1928 | |
| 1929 | rettv->v_type = VAR_UNKNOWN; |
| 1930 | switch (**arg) |
| 1931 | { |
| 1932 | /* |
| 1933 | * Number constant. |
| 1934 | */ |
| 1935 | case '0': // also for blob starting with 0z |
| 1936 | case '1': |
| 1937 | case '2': |
| 1938 | case '3': |
| 1939 | case '4': |
| 1940 | case '5': |
| 1941 | case '6': |
| 1942 | case '7': |
| 1943 | case '8': |
| 1944 | case '9': |
| 1945 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
| 1946 | return FAIL; |
| 1947 | // Apply "-" and "+" just before the number now, right to |
| 1948 | // left. Matters especially when "->" follows. Stops at |
| 1949 | // '!'. |
| 1950 | if (apply_leader(rettv, TRUE, |
| 1951 | start_leader, &end_leader) == FAIL) |
| 1952 | { |
| 1953 | clear_tv(rettv); |
| 1954 | return FAIL; |
| 1955 | } |
| 1956 | break; |
| 1957 | |
| 1958 | /* |
| 1959 | * String constant: "string". |
| 1960 | */ |
| 1961 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
| 1962 | return FAIL; |
| 1963 | break; |
| 1964 | |
| 1965 | /* |
| 1966 | * Literal string constant: 'str''ing'. |
| 1967 | */ |
| 1968 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
| 1969 | return FAIL; |
| 1970 | break; |
| 1971 | |
| 1972 | /* |
| 1973 | * Constant Vim variable. |
| 1974 | */ |
| 1975 | case 'v': get_vim_constant(arg, rettv); |
| 1976 | ret = NOTDONE; |
| 1977 | break; |
| 1978 | |
| 1979 | /* |
| 1980 | * "true" constant |
| 1981 | */ |
| 1982 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 1983 | && !eval_isnamec((*arg)[4])) |
| 1984 | { |
| 1985 | *arg += 4; |
| 1986 | rettv->v_type = VAR_BOOL; |
| 1987 | rettv->vval.v_number = VVAL_TRUE; |
| 1988 | } |
| 1989 | else |
| 1990 | ret = NOTDONE; |
| 1991 | break; |
| 1992 | |
| 1993 | /* |
| 1994 | * "false" constant |
| 1995 | */ |
| 1996 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 1997 | && !eval_isnamec((*arg)[5])) |
| 1998 | { |
| 1999 | *arg += 5; |
| 2000 | rettv->v_type = VAR_BOOL; |
| 2001 | rettv->vval.v_number = VVAL_FALSE; |
| 2002 | } |
| 2003 | else |
| 2004 | ret = NOTDONE; |
| 2005 | break; |
| 2006 | |
| 2007 | /* |
| 2008 | * "null" constant |
| 2009 | */ |
| 2010 | case 'n': if (STRNCMP(*arg, "null", 4) == 0 |
| 2011 | && !eval_isnamec((*arg)[4])) |
| 2012 | { |
| 2013 | *arg += 4; |
| 2014 | rettv->v_type = VAR_SPECIAL; |
| 2015 | rettv->vval.v_number = VVAL_NULL; |
| 2016 | } |
| 2017 | else |
| 2018 | ret = NOTDONE; |
| 2019 | break; |
| 2020 | |
| 2021 | /* |
| 2022 | * List: [expr, expr] |
| 2023 | */ |
| 2024 | case '[': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2025 | return FAIL; |
| 2026 | ret = compile_list(arg, cctx, ppconst); |
| 2027 | break; |
| 2028 | |
| 2029 | /* |
| 2030 | * Dictionary: {'key': val, 'key': val} |
| 2031 | */ |
| 2032 | case '{': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2033 | return FAIL; |
| 2034 | ret = compile_dict(arg, cctx, ppconst); |
| 2035 | break; |
| 2036 | |
| 2037 | /* |
| 2038 | * Option value: &name |
| 2039 | */ |
| 2040 | case '&': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2041 | return FAIL; |
| 2042 | ret = compile_get_option(arg, cctx); |
| 2043 | break; |
| 2044 | |
| 2045 | /* |
| 2046 | * Environment variable: $VAR. |
| 2047 | */ |
| 2048 | case '$': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2049 | return FAIL; |
| 2050 | ret = compile_get_env(arg, cctx); |
| 2051 | break; |
| 2052 | |
| 2053 | /* |
| 2054 | * Register contents: @r. |
| 2055 | */ |
| 2056 | case '@': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2057 | return FAIL; |
| 2058 | ret = compile_get_register(arg, cctx); |
| 2059 | break; |
| 2060 | /* |
| 2061 | * nested expression: (expression). |
| 2062 | * lambda: (arg, arg) => expr |
| 2063 | * funcref: (arg, arg) => { statement } |
| 2064 | */ |
| 2065 | case '(': // if compile_lambda returns NOTDONE then it must be (expr) |
| 2066 | ret = compile_lambda(arg, cctx); |
| 2067 | if (ret == NOTDONE) |
| 2068 | ret = compile_parenthesis(arg, cctx, ppconst); |
| 2069 | break; |
| 2070 | |
| 2071 | default: ret = NOTDONE; |
| 2072 | break; |
| 2073 | } |
| 2074 | if (ret == FAIL) |
| 2075 | return FAIL; |
| 2076 | |
| 2077 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
| 2078 | { |
| 2079 | if (cctx->ctx_skip == SKIP_YES) |
| 2080 | clear_tv(rettv); |
| 2081 | else |
| 2082 | // A constant expression can possibly be handled compile time, |
| 2083 | // return the value instead of generating code. |
| 2084 | ++ppconst->pp_used; |
| 2085 | } |
| 2086 | else if (ret == NOTDONE) |
| 2087 | { |
| 2088 | char_u *p; |
| 2089 | int r; |
| 2090 | |
| 2091 | if (!eval_isnamec1(**arg)) |
| 2092 | { |
| 2093 | if (!vim9_bad_comment(*arg)) |
| 2094 | { |
| 2095 | if (ends_excmd(*skipwhite(*arg))) |
| 2096 | semsg(_(e_empty_expression_str), *arg); |
| 2097 | else |
| 2098 | semsg(_(e_name_expected_str), *arg); |
| 2099 | } |
| 2100 | return FAIL; |
| 2101 | } |
| 2102 | |
| 2103 | // "name" or "name()" |
| 2104 | p = to_name_end(*arg, TRUE); |
| 2105 | if (p - *arg == (size_t)1 && **arg == '_') |
| 2106 | { |
| 2107 | emsg(_(e_cannot_use_underscore_here)); |
| 2108 | return FAIL; |
| 2109 | } |
| 2110 | |
| 2111 | if (*p == '(') |
| 2112 | { |
| 2113 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 2114 | } |
| 2115 | else |
| 2116 | { |
| 2117 | if (cctx->ctx_skip != SKIP_YES |
| 2118 | && generate_ppconst(cctx, ppconst) == FAIL) |
| 2119 | return FAIL; |
| 2120 | r = compile_load(arg, p, cctx, TRUE, TRUE); |
| 2121 | } |
| 2122 | if (r == FAIL) |
| 2123 | return FAIL; |
| 2124 | } |
| 2125 | |
| 2126 | // Handle following "[]", ".member", etc. |
| 2127 | // Then deal with prefixed '-', '+' and '!', if not done already. |
| 2128 | if (compile_subscript(arg, cctx, start_leader, &end_leader, |
| 2129 | ppconst) == FAIL) |
| 2130 | return FAIL; |
| 2131 | if (ppconst->pp_used > 0) |
| 2132 | { |
| 2133 | // apply the '!', '-' and '+' before the constant |
| 2134 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 2135 | if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL) |
| 2136 | return FAIL; |
| 2137 | return OK; |
| 2138 | } |
| 2139 | if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL) |
| 2140 | return FAIL; |
| 2141 | return OK; |
| 2142 | } |
| 2143 | |
| 2144 | /* |
Bram Moolenaar | 5da3605 | 2021-12-27 15:39:57 +0000 | [diff] [blame] | 2145 | * <type>expr8: runtime type check / conversion |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2146 | */ |
| 2147 | static int |
Bram Moolenaar | 5da3605 | 2021-12-27 15:39:57 +0000 | [diff] [blame] | 2148 | compile_expr7(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2149 | { |
| 2150 | type_T *want_type = NULL; |
| 2151 | |
| 2152 | // Recognize <type> |
| 2153 | if (**arg == '<' && eval_isnamec1((*arg)[1])) |
| 2154 | { |
| 2155 | ++*arg; |
| 2156 | want_type = parse_type(arg, cctx->ctx_type_list, TRUE); |
| 2157 | if (want_type == NULL) |
| 2158 | return FAIL; |
| 2159 | |
| 2160 | if (**arg != '>') |
| 2161 | { |
| 2162 | if (*skipwhite(*arg) == '>') |
| 2163 | semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg); |
| 2164 | else |
| 2165 | emsg(_(e_missing_gt)); |
| 2166 | return FAIL; |
| 2167 | } |
| 2168 | ++*arg; |
| 2169 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
| 2170 | return FAIL; |
| 2171 | } |
| 2172 | |
Bram Moolenaar | 5da3605 | 2021-12-27 15:39:57 +0000 | [diff] [blame] | 2173 | if (compile_expr8(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2174 | return FAIL; |
| 2175 | |
| 2176 | if (want_type != NULL) |
| 2177 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2178 | type_T *actual; |
| 2179 | where_T where = WHERE_INIT; |
| 2180 | |
| 2181 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2182 | actual = get_type_on_stack(cctx, 0); |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 2183 | if (check_type_maybe(want_type, actual, FALSE, where) != OK) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2184 | { |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 2185 | if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) |
| 2186 | == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2187 | return FAIL; |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | return OK; |
| 2192 | } |
| 2193 | |
| 2194 | /* |
| 2195 | * * number multiplication |
| 2196 | * / number division |
| 2197 | * % number modulo |
| 2198 | */ |
| 2199 | static int |
| 2200 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 2201 | { |
| 2202 | char_u *op; |
| 2203 | char_u *next; |
| 2204 | int ppconst_used = ppconst->pp_used; |
| 2205 | |
| 2206 | // get the first expression |
Bram Moolenaar | 5da3605 | 2021-12-27 15:39:57 +0000 | [diff] [blame] | 2207 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2208 | return FAIL; |
| 2209 | |
| 2210 | /* |
| 2211 | * Repeat computing, until no "*", "/" or "%" is following. |
| 2212 | */ |
| 2213 | for (;;) |
| 2214 | { |
| 2215 | op = may_peek_next_line(cctx, *arg, &next); |
| 2216 | if (*op != '*' && *op != '/' && *op != '%') |
| 2217 | break; |
| 2218 | if (next != NULL) |
| 2219 | { |
| 2220 | *arg = next_line_from_context(cctx, TRUE); |
| 2221 | op = skipwhite(*arg); |
| 2222 | } |
| 2223 | |
| 2224 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
| 2225 | { |
| 2226 | error_white_both(op, 1); |
| 2227 | return FAIL; |
| 2228 | } |
| 2229 | if (may_get_next_line_error(op + 1, arg, cctx) == FAIL) |
| 2230 | return FAIL; |
| 2231 | |
| 2232 | // get the second expression |
Bram Moolenaar | 5da3605 | 2021-12-27 15:39:57 +0000 | [diff] [blame] | 2233 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2234 | return FAIL; |
| 2235 | |
| 2236 | if (ppconst->pp_used == ppconst_used + 2 |
| 2237 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 2238 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
| 2239 | { |
| 2240 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 2241 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
| 2242 | varnumber_T res = 0; |
| 2243 | int failed = FALSE; |
| 2244 | |
| 2245 | // both are numbers: compute the result |
| 2246 | switch (*op) |
| 2247 | { |
| 2248 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
| 2249 | break; |
| 2250 | case '/': res = num_divide(tv1->vval.v_number, |
| 2251 | tv2->vval.v_number, &failed); |
| 2252 | break; |
| 2253 | case '%': res = num_modulus(tv1->vval.v_number, |
| 2254 | tv2->vval.v_number, &failed); |
| 2255 | break; |
| 2256 | } |
| 2257 | if (failed) |
| 2258 | return FAIL; |
| 2259 | tv1->vval.v_number = res; |
| 2260 | --ppconst->pp_used; |
| 2261 | } |
| 2262 | else |
| 2263 | { |
| 2264 | generate_ppconst(cctx, ppconst); |
| 2265 | generate_two_op(cctx, op); |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | return OK; |
| 2270 | } |
| 2271 | |
| 2272 | /* |
| 2273 | * + number addition or list/blobl concatenation |
| 2274 | * - number subtraction |
| 2275 | * .. string concatenation |
| 2276 | */ |
| 2277 | static int |
| 2278 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 2279 | { |
| 2280 | char_u *op; |
| 2281 | char_u *next; |
| 2282 | int oplen; |
| 2283 | int ppconst_used = ppconst->pp_used; |
| 2284 | |
| 2285 | // get the first variable |
| 2286 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
| 2287 | return FAIL; |
| 2288 | |
| 2289 | /* |
| 2290 | * Repeat computing, until no "+", "-" or ".." is following. |
| 2291 | */ |
| 2292 | for (;;) |
| 2293 | { |
| 2294 | op = may_peek_next_line(cctx, *arg, &next); |
| 2295 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
| 2296 | break; |
| 2297 | if (op[0] == op[1] && *op != '.' && next) |
| 2298 | // Finding "++" or "--" on the next line is a separate command. |
| 2299 | // But ".." is concatenation. |
| 2300 | break; |
| 2301 | oplen = (*op == '.' ? 2 : 1); |
| 2302 | if (next != NULL) |
| 2303 | { |
| 2304 | *arg = next_line_from_context(cctx, TRUE); |
| 2305 | op = skipwhite(*arg); |
| 2306 | } |
| 2307 | |
| 2308 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
| 2309 | { |
| 2310 | error_white_both(op, oplen); |
| 2311 | return FAIL; |
| 2312 | } |
| 2313 | |
| 2314 | if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL) |
| 2315 | return FAIL; |
| 2316 | |
| 2317 | // get the second expression |
| 2318 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
| 2319 | return FAIL; |
| 2320 | |
| 2321 | if (ppconst->pp_used == ppconst_used + 2 |
| 2322 | && (*op == '.' |
| 2323 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 2324 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 2325 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 2326 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
| 2327 | { |
| 2328 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 2329 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
| 2330 | |
| 2331 | // concat/subtract/add constant numbers |
| 2332 | if (*op == '+') |
| 2333 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 2334 | else if (*op == '-') |
| 2335 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 2336 | else |
| 2337 | { |
| 2338 | // concatenate constant strings |
| 2339 | char_u *s1 = tv1->vval.v_string; |
| 2340 | char_u *s2 = tv2->vval.v_string; |
| 2341 | size_t len1 = STRLEN(s1); |
| 2342 | |
| 2343 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 2344 | if (tv1->vval.v_string == NULL) |
| 2345 | { |
| 2346 | clear_ppconst(ppconst); |
| 2347 | return FAIL; |
| 2348 | } |
| 2349 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 2350 | STRCPY(tv1->vval.v_string + len1, s2); |
| 2351 | vim_free(s1); |
| 2352 | vim_free(s2); |
| 2353 | } |
| 2354 | --ppconst->pp_used; |
| 2355 | } |
| 2356 | else |
| 2357 | { |
| 2358 | generate_ppconst(cctx, ppconst); |
| 2359 | ppconst->pp_is_const = FALSE; |
| 2360 | if (*op == '.') |
| 2361 | { |
| 2362 | if (may_generate_2STRING(-2, FALSE, cctx) == FAIL |
| 2363 | || may_generate_2STRING(-1, FALSE, cctx) == FAIL) |
| 2364 | return FAIL; |
| 2365 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 2366 | } |
| 2367 | else |
| 2368 | generate_two_op(cctx, op); |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | return OK; |
| 2373 | } |
| 2374 | |
| 2375 | /* |
| 2376 | * expr5a == expr5b |
| 2377 | * expr5a =~ expr5b |
| 2378 | * expr5a != expr5b |
| 2379 | * expr5a !~ expr5b |
| 2380 | * expr5a > expr5b |
| 2381 | * expr5a >= expr5b |
| 2382 | * expr5a < expr5b |
| 2383 | * expr5a <= expr5b |
| 2384 | * expr5a is expr5b |
| 2385 | * expr5a isnot expr5b |
| 2386 | * |
| 2387 | * Produces instructions: |
| 2388 | * EVAL expr5a Push result of "expr5a" |
| 2389 | * EVAL expr5b Push result of "expr5b" |
| 2390 | * COMPARE one of the compare instructions |
| 2391 | */ |
| 2392 | static int |
| 2393 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 2394 | { |
| 2395 | exprtype_T type = EXPR_UNKNOWN; |
| 2396 | char_u *p; |
| 2397 | char_u *next; |
| 2398 | int len = 2; |
| 2399 | int type_is = FALSE; |
| 2400 | int ppconst_used = ppconst->pp_used; |
| 2401 | |
| 2402 | // get the first variable |
| 2403 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
| 2404 | return FAIL; |
| 2405 | |
| 2406 | p = may_peek_next_line(cctx, *arg, &next); |
| 2407 | type = get_compare_type(p, &len, &type_is); |
| 2408 | |
| 2409 | /* |
| 2410 | * If there is a comparative operator, use it. |
| 2411 | */ |
| 2412 | if (type != EXPR_UNKNOWN) |
| 2413 | { |
| 2414 | int ic = FALSE; // Default: do not ignore case |
| 2415 | |
| 2416 | if (next != NULL) |
| 2417 | { |
| 2418 | *arg = next_line_from_context(cctx, TRUE); |
| 2419 | p = skipwhite(*arg); |
| 2420 | } |
| 2421 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 2422 | { |
| 2423 | semsg(_(e_invalid_expression_str), *arg); |
| 2424 | return FAIL; |
| 2425 | } |
| 2426 | // extra question mark appended: ignore case |
| 2427 | if (p[len] == '?') |
| 2428 | { |
| 2429 | ic = TRUE; |
| 2430 | ++len; |
| 2431 | } |
| 2432 | // extra '#' appended: match case (ignored) |
| 2433 | else if (p[len] == '#') |
| 2434 | ++len; |
| 2435 | // nothing appended: match case |
| 2436 | |
| 2437 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
| 2438 | { |
| 2439 | error_white_both(p, len); |
| 2440 | return FAIL; |
| 2441 | } |
| 2442 | |
| 2443 | // get the second variable |
| 2444 | if (may_get_next_line_error(p + len, arg, cctx) == FAIL) |
| 2445 | return FAIL; |
| 2446 | |
| 2447 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
| 2448 | return FAIL; |
| 2449 | |
| 2450 | if (ppconst->pp_used == ppconst_used + 2) |
| 2451 | { |
| 2452 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 2453 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 2454 | int ret; |
| 2455 | |
| 2456 | // Both sides are a constant, compute the result now. |
| 2457 | // First check for a valid combination of types, this is more |
| 2458 | // strict than typval_compare(). |
| 2459 | if (check_compare_types(type, tv1, tv2) == FAIL) |
| 2460 | ret = FAIL; |
| 2461 | else |
| 2462 | { |
| 2463 | ret = typval_compare(tv1, tv2, type, ic); |
| 2464 | tv1->v_type = VAR_BOOL; |
| 2465 | tv1->vval.v_number = tv1->vval.v_number |
| 2466 | ? VVAL_TRUE : VVAL_FALSE; |
| 2467 | clear_tv(tv2); |
| 2468 | --ppconst->pp_used; |
| 2469 | } |
| 2470 | return ret; |
| 2471 | } |
| 2472 | |
| 2473 | generate_ppconst(cctx, ppconst); |
| 2474 | return generate_COMPARE(cctx, type, ic); |
| 2475 | } |
| 2476 | |
| 2477 | return OK; |
| 2478 | } |
| 2479 | |
| 2480 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2481 | |
| 2482 | /* |
| 2483 | * Compile || or &&. |
| 2484 | */ |
| 2485 | static int |
| 2486 | compile_and_or( |
| 2487 | char_u **arg, |
| 2488 | cctx_T *cctx, |
| 2489 | char *op, |
| 2490 | ppconst_T *ppconst, |
| 2491 | int ppconst_used UNUSED) |
| 2492 | { |
| 2493 | char_u *next; |
| 2494 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
| 2495 | int opchar = *op; |
| 2496 | |
| 2497 | if (p[0] == opchar && p[1] == opchar) |
| 2498 | { |
| 2499 | garray_T *instr = &cctx->ctx_instr; |
| 2500 | garray_T end_ga; |
| 2501 | int save_skip = cctx->ctx_skip; |
| 2502 | |
| 2503 | /* |
| 2504 | * Repeat until there is no following "||" or "&&" |
| 2505 | */ |
| 2506 | ga_init2(&end_ga, sizeof(int), 10); |
| 2507 | while (p[0] == opchar && p[1] == opchar) |
| 2508 | { |
| 2509 | long start_lnum = SOURCING_LNUM; |
| 2510 | long save_sourcing_lnum; |
| 2511 | int start_ctx_lnum = cctx->ctx_lnum; |
| 2512 | int save_lnum; |
| 2513 | int const_used; |
| 2514 | int status; |
| 2515 | jumpwhen_T jump_when = opchar == '|' |
| 2516 | ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE; |
| 2517 | |
| 2518 | if (next != NULL) |
| 2519 | { |
| 2520 | *arg = next_line_from_context(cctx, TRUE); |
| 2521 | p = skipwhite(*arg); |
| 2522 | } |
| 2523 | |
| 2524 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 2525 | { |
| 2526 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 2527 | op, p); |
| 2528 | ga_clear(&end_ga); |
| 2529 | return FAIL; |
| 2530 | } |
| 2531 | |
| 2532 | save_sourcing_lnum = SOURCING_LNUM; |
| 2533 | SOURCING_LNUM = start_lnum; |
| 2534 | save_lnum = cctx->ctx_lnum; |
| 2535 | cctx->ctx_lnum = start_ctx_lnum; |
| 2536 | |
| 2537 | status = check_ppconst_bool(ppconst); |
| 2538 | if (status != FAIL) |
| 2539 | { |
| 2540 | // Use the last ppconst if possible. |
| 2541 | if (ppconst->pp_used > 0) |
| 2542 | { |
| 2543 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 2544 | int is_true = tv2bool(tv); |
| 2545 | |
| 2546 | if ((is_true && opchar == '|') |
| 2547 | || (!is_true && opchar == '&')) |
| 2548 | { |
| 2549 | // For "false && expr" and "true || expr" the "expr" |
| 2550 | // does not need to be evaluated. |
| 2551 | cctx->ctx_skip = SKIP_YES; |
| 2552 | clear_tv(tv); |
| 2553 | tv->v_type = VAR_BOOL; |
| 2554 | tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE; |
| 2555 | } |
| 2556 | else |
| 2557 | { |
| 2558 | // For "true && expr" and "false || expr" only "expr" |
| 2559 | // needs to be evaluated. |
| 2560 | --ppconst->pp_used; |
| 2561 | jump_when = JUMP_NEVER; |
| 2562 | } |
| 2563 | } |
| 2564 | else |
| 2565 | { |
| 2566 | // Every part must evaluate to a bool. |
| 2567 | status = bool_on_stack(cctx); |
| 2568 | } |
| 2569 | } |
| 2570 | if (status != FAIL) |
| 2571 | status = ga_grow(&end_ga, 1); |
| 2572 | cctx->ctx_lnum = save_lnum; |
| 2573 | if (status == FAIL) |
| 2574 | { |
| 2575 | ga_clear(&end_ga); |
| 2576 | return FAIL; |
| 2577 | } |
| 2578 | |
| 2579 | if (jump_when != JUMP_NEVER) |
| 2580 | { |
| 2581 | if (cctx->ctx_skip != SKIP_YES) |
| 2582 | { |
| 2583 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 2584 | ++end_ga.ga_len; |
| 2585 | } |
| 2586 | generate_JUMP(cctx, jump_when, 0); |
| 2587 | } |
| 2588 | |
| 2589 | // eval the next expression |
| 2590 | SOURCING_LNUM = save_sourcing_lnum; |
| 2591 | if (may_get_next_line_error(p + 2, arg, cctx) == FAIL) |
| 2592 | { |
| 2593 | ga_clear(&end_ga); |
| 2594 | return FAIL; |
| 2595 | } |
| 2596 | |
| 2597 | const_used = ppconst->pp_used; |
| 2598 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 2599 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
| 2600 | { |
| 2601 | ga_clear(&end_ga); |
| 2602 | return FAIL; |
| 2603 | } |
| 2604 | |
| 2605 | // "0 || 1" results in true, "1 && 0" results in false. |
| 2606 | if (ppconst->pp_used == const_used + 1) |
| 2607 | { |
| 2608 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 2609 | |
| 2610 | if (tv->v_type == VAR_NUMBER |
| 2611 | && (tv->vval.v_number == 1 || tv->vval.v_number == 0)) |
| 2612 | { |
| 2613 | tv->vval.v_number = tv->vval.v_number == 1 |
| 2614 | ? VVAL_TRUE : VVAL_FALSE; |
| 2615 | tv->v_type = VAR_BOOL; |
| 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | p = may_peek_next_line(cctx, *arg, &next); |
| 2620 | } |
| 2621 | |
| 2622 | if (check_ppconst_bool(ppconst) == FAIL) |
| 2623 | { |
| 2624 | ga_clear(&end_ga); |
| 2625 | return FAIL; |
| 2626 | } |
| 2627 | |
| 2628 | if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0) |
| 2629 | // Every part must evaluate to a bool. |
| 2630 | if (bool_on_stack(cctx) == FAIL) |
| 2631 | { |
| 2632 | ga_clear(&end_ga); |
| 2633 | return FAIL; |
| 2634 | } |
| 2635 | |
| 2636 | if (end_ga.ga_len > 0) |
| 2637 | { |
| 2638 | // Fill in the end label in all jumps. |
| 2639 | generate_ppconst(cctx, ppconst); |
| 2640 | while (end_ga.ga_len > 0) |
| 2641 | { |
| 2642 | isn_T *isn; |
| 2643 | |
| 2644 | --end_ga.ga_len; |
| 2645 | isn = ((isn_T *)instr->ga_data) |
| 2646 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 2647 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 2648 | } |
| 2649 | } |
| 2650 | ga_clear(&end_ga); |
| 2651 | |
| 2652 | cctx->ctx_skip = save_skip; |
| 2653 | } |
| 2654 | |
| 2655 | return OK; |
| 2656 | } |
| 2657 | |
| 2658 | /* |
| 2659 | * expr4a && expr4a && expr4a logical AND |
| 2660 | * |
| 2661 | * Produces instructions: |
| 2662 | * EVAL expr4a Push result of "expr4a" |
| 2663 | * COND2BOOL convert to bool if needed |
| 2664 | * JUMP_IF_COND_FALSE end |
| 2665 | * EVAL expr4b Push result of "expr4b" |
| 2666 | * JUMP_IF_COND_FALSE end |
| 2667 | * EVAL expr4c Push result of "expr4c" |
| 2668 | * end: |
| 2669 | */ |
| 2670 | static int |
| 2671 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 2672 | { |
| 2673 | int ppconst_used = ppconst->pp_used; |
| 2674 | |
| 2675 | // get the first variable |
| 2676 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
| 2677 | return FAIL; |
| 2678 | |
| 2679 | // || and && work almost the same |
| 2680 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
| 2681 | } |
| 2682 | |
| 2683 | /* |
| 2684 | * expr3a || expr3b || expr3c logical OR |
| 2685 | * |
| 2686 | * Produces instructions: |
| 2687 | * EVAL expr3a Push result of "expr3a" |
| 2688 | * COND2BOOL convert to bool if needed |
| 2689 | * JUMP_IF_COND_TRUE end |
| 2690 | * EVAL expr3b Push result of "expr3b" |
| 2691 | * JUMP_IF_COND_TRUE end |
| 2692 | * EVAL expr3c Push result of "expr3c" |
| 2693 | * end: |
| 2694 | */ |
| 2695 | static int |
| 2696 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 2697 | { |
| 2698 | int ppconst_used = ppconst->pp_used; |
| 2699 | |
| 2700 | // eval the first expression |
| 2701 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
| 2702 | return FAIL; |
| 2703 | |
| 2704 | // || and && work almost the same |
| 2705 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
| 2706 | } |
| 2707 | |
| 2708 | /* |
| 2709 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 2710 | * Produces instructions: |
| 2711 | * EVAL expr2 Push result of "expr2" |
| 2712 | * JUMP_IF_FALSE alt jump if false |
| 2713 | * EVAL expr1a |
| 2714 | * JUMP_ALWAYS end |
| 2715 | * alt: EVAL expr1b |
| 2716 | * end: |
| 2717 | * |
| 2718 | * Toplevel expression: expr2 ?? expr1 |
| 2719 | * Produces instructions: |
| 2720 | * EVAL expr2 Push result of "expr2" |
| 2721 | * JUMP_AND_KEEP_IF_TRUE end jump if true |
| 2722 | * EVAL expr1 |
| 2723 | * end: |
| 2724 | */ |
| 2725 | int |
| 2726 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 2727 | { |
| 2728 | char_u *p; |
| 2729 | int ppconst_used = ppconst->pp_used; |
| 2730 | char_u *next; |
| 2731 | |
| 2732 | // Ignore all kinds of errors when not producing code. |
| 2733 | if (cctx->ctx_skip == SKIP_YES) |
| 2734 | { |
| 2735 | skip_expr_cctx(arg, cctx); |
| 2736 | return OK; |
| 2737 | } |
| 2738 | |
| 2739 | // Evaluate the first expression. |
| 2740 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
| 2741 | return FAIL; |
| 2742 | |
| 2743 | p = may_peek_next_line(cctx, *arg, &next); |
| 2744 | if (*p == '?') |
| 2745 | { |
| 2746 | int op_falsy = p[1] == '?'; |
| 2747 | garray_T *instr = &cctx->ctx_instr; |
| 2748 | garray_T *stack = &cctx->ctx_type_stack; |
| 2749 | int alt_idx = instr->ga_len; |
| 2750 | int end_idx = 0; |
| 2751 | isn_T *isn; |
| 2752 | type_T *type1 = NULL; |
| 2753 | int has_const_expr = FALSE; |
| 2754 | int const_value = FALSE; |
| 2755 | int save_skip = cctx->ctx_skip; |
| 2756 | |
| 2757 | if (next != NULL) |
| 2758 | { |
| 2759 | *arg = next_line_from_context(cctx, TRUE); |
| 2760 | p = skipwhite(*arg); |
| 2761 | } |
| 2762 | |
| 2763 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy])) |
| 2764 | { |
| 2765 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 2766 | op_falsy ? "??" : "?", p); |
| 2767 | return FAIL; |
| 2768 | } |
| 2769 | |
| 2770 | if (ppconst->pp_used == ppconst_used + 1) |
| 2771 | { |
| 2772 | // the condition is a constant, we know whether the ? or the : |
| 2773 | // expression is to be evaluated. |
| 2774 | has_const_expr = TRUE; |
| 2775 | if (op_falsy) |
| 2776 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 2777 | else |
| 2778 | { |
| 2779 | int error = FALSE; |
| 2780 | |
| 2781 | const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used], |
| 2782 | &error); |
| 2783 | if (error) |
| 2784 | return FAIL; |
| 2785 | } |
| 2786 | cctx->ctx_skip = save_skip == SKIP_YES || |
| 2787 | (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT; |
| 2788 | |
| 2789 | if (op_falsy && cctx->ctx_skip == SKIP_YES) |
| 2790 | // "left ?? right" and "left" is truthy: produce "left" |
| 2791 | generate_ppconst(cctx, ppconst); |
| 2792 | else |
| 2793 | { |
| 2794 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 2795 | --ppconst->pp_used; |
| 2796 | } |
| 2797 | } |
| 2798 | else |
| 2799 | { |
| 2800 | generate_ppconst(cctx, ppconst); |
| 2801 | if (op_falsy) |
| 2802 | end_idx = instr->ga_len; |
| 2803 | generate_JUMP(cctx, op_falsy |
| 2804 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0); |
| 2805 | if (op_falsy) |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2806 | type1 = get_type_on_stack(cctx, -1); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | // evaluate the second expression; any type is accepted |
| 2810 | if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL) |
| 2811 | return FAIL; |
| 2812 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
| 2813 | return FAIL; |
| 2814 | |
| 2815 | if (!has_const_expr) |
| 2816 | { |
| 2817 | generate_ppconst(cctx, ppconst); |
| 2818 | |
| 2819 | if (!op_falsy) |
| 2820 | { |
| 2821 | // remember the type and drop it |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2822 | type1 = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2823 | --stack->ga_len; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2824 | |
| 2825 | end_idx = instr->ga_len; |
| 2826 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 2827 | |
| 2828 | // jump here from JUMP_IF_FALSE |
| 2829 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 2830 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | if (!op_falsy) |
| 2835 | { |
| 2836 | // Check for the ":". |
| 2837 | p = may_peek_next_line(cctx, *arg, &next); |
| 2838 | if (*p != ':') |
| 2839 | { |
| 2840 | emsg(_(e_missing_colon_after_questionmark)); |
| 2841 | return FAIL; |
| 2842 | } |
| 2843 | if (next != NULL) |
| 2844 | { |
| 2845 | *arg = next_line_from_context(cctx, TRUE); |
| 2846 | p = skipwhite(*arg); |
| 2847 | } |
| 2848 | |
| 2849 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 2850 | { |
| 2851 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 2852 | ":", p); |
| 2853 | return FAIL; |
| 2854 | } |
| 2855 | |
| 2856 | // evaluate the third expression |
| 2857 | if (has_const_expr) |
| 2858 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 2859 | ? SKIP_YES : SKIP_NOT; |
| 2860 | if (may_get_next_line_error(p + 1, arg, cctx) == FAIL) |
| 2861 | return FAIL; |
| 2862 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
| 2863 | return FAIL; |
| 2864 | } |
| 2865 | |
| 2866 | if (!has_const_expr) |
| 2867 | { |
| 2868 | type_T **typep; |
| 2869 | |
| 2870 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | fa46ead | 2021-12-22 13:18:39 +0000 | [diff] [blame] | 2871 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2872 | |
| 2873 | // If the types differ, the result has a more generic type. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2874 | typep = &((((type2_T *)stack->ga_data) |
| 2875 | + stack->ga_len - 1)->type_curr); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2876 | common_type(type1, *typep, typep, cctx->ctx_type_list); |
| 2877 | |
| 2878 | // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE |
| 2879 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 2880 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 2881 | } |
| 2882 | |
| 2883 | cctx->ctx_skip = save_skip; |
| 2884 | } |
| 2885 | return OK; |
| 2886 | } |
| 2887 | |
| 2888 | /* |
| 2889 | * Toplevel expression. |
| 2890 | * Sets "is_const" (if not NULL) to indicate the value is a constant. |
| 2891 | * Returns OK or FAIL. |
| 2892 | */ |
| 2893 | int |
| 2894 | compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const) |
| 2895 | { |
| 2896 | ppconst_T ppconst; |
| 2897 | |
| 2898 | CLEAR_FIELD(ppconst); |
| 2899 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 2900 | { |
| 2901 | clear_ppconst(&ppconst); |
| 2902 | return FAIL; |
| 2903 | } |
| 2904 | if (is_const != NULL) |
| 2905 | *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const; |
| 2906 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 2907 | return FAIL; |
| 2908 | return OK; |
| 2909 | } |
| 2910 | |
| 2911 | /* |
| 2912 | * Toplevel expression. |
| 2913 | */ |
| 2914 | int |
| 2915 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 2916 | { |
| 2917 | return compile_expr0_ext(arg, cctx, NULL); |
| 2918 | } |
| 2919 | |
| 2920 | |
| 2921 | #endif // defined(FEAT_EVAL) |