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