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