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