Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9cmds.c: Dealing with commands of a compiled function |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | // When not generating protos this is included in proto.h |
| 20 | #ifdef PROTO |
| 21 | # include "vim9.h" |
| 22 | #endif |
| 23 | |
| 24 | /* |
| 25 | * Get the index of the current instruction. |
| 26 | * This compensates for a preceding ISN_CMDMOD and ISN_PROF_START. |
| 27 | */ |
| 28 | static int |
| 29 | current_instr_idx(cctx_T *cctx) |
| 30 | { |
| 31 | garray_T *instr = &cctx->ctx_instr; |
| 32 | int idx = instr->ga_len; |
| 33 | |
| 34 | while (idx > 0) |
| 35 | { |
| 36 | if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1] |
| 37 | .isn_type == ISN_CMDMOD) |
| 38 | { |
| 39 | --idx; |
| 40 | continue; |
| 41 | } |
| 42 | #ifdef FEAT_PROFILE |
| 43 | if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START) |
| 44 | { |
| 45 | --idx; |
| 46 | continue; |
| 47 | } |
| 48 | #endif |
| 49 | if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG) |
| 50 | { |
| 51 | --idx; |
| 52 | continue; |
| 53 | } |
| 54 | break; |
| 55 | } |
| 56 | return idx; |
| 57 | } |
| 58 | /* |
| 59 | * Remove local variables above "new_top". |
| 60 | */ |
| 61 | static void |
| 62 | unwind_locals(cctx_T *cctx, int new_top) |
| 63 | { |
| 64 | if (cctx->ctx_locals.ga_len > new_top) |
| 65 | { |
| 66 | int idx; |
| 67 | lvar_T *lvar; |
| 68 | |
| 69 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 70 | { |
| 71 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 72 | vim_free(lvar->lv_name); |
| 73 | } |
| 74 | } |
| 75 | cctx->ctx_locals.ga_len = new_top; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Free all local variables. |
| 80 | */ |
| 81 | void |
| 82 | free_locals(cctx_T *cctx) |
| 83 | { |
| 84 | unwind_locals(cctx, 0); |
| 85 | ga_clear(&cctx->ctx_locals); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /* |
| 90 | * Check if "name" can be "unlet". |
| 91 | */ |
| 92 | int |
| 93 | check_vim9_unlet(char_u *name) |
| 94 | { |
Bram Moolenaar | dbdd16b | 2022-08-14 21:46:07 +0100 | [diff] [blame] | 95 | if (*name == NUL) |
| 96 | { |
| 97 | semsg(_(e_argument_required_for_str), "unlet"); |
| 98 | return FAIL; |
| 99 | } |
| 100 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 101 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 102 | { |
| 103 | // "unlet s:var" is allowed in legacy script. |
| 104 | if (*name == 's' && !script_is_vim9()) |
| 105 | return OK; |
| 106 | semsg(_(e_cannot_unlet_str), name); |
| 107 | return FAIL; |
| 108 | } |
| 109 | return OK; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Callback passed to ex_unletlock(). |
| 114 | */ |
| 115 | static int |
| 116 | compile_unlet( |
| 117 | lval_T *lvp, |
| 118 | char_u *name_end, |
| 119 | exarg_T *eap, |
| 120 | int deep UNUSED, |
| 121 | void *coookie) |
| 122 | { |
| 123 | cctx_T *cctx = coookie; |
| 124 | char_u *p = lvp->ll_name; |
| 125 | int cc = *name_end; |
| 126 | int ret = OK; |
| 127 | |
| 128 | if (cctx->ctx_skip == SKIP_YES) |
| 129 | return OK; |
| 130 | |
| 131 | *name_end = NUL; |
| 132 | if (*p == '$') |
| 133 | { |
| 134 | // :unlet $ENV_VAR |
| 135 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 136 | } |
| 137 | else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL) |
| 138 | { |
| 139 | lhs_T lhs; |
| 140 | |
| 141 | // This is similar to assigning: lookup the list/dict, compile the |
| 142 | // idx/key. Then instead of storing the value unlet the item. |
| 143 | // unlet {list}[idx] |
| 144 | // unlet {dict}[key] dict.key |
| 145 | // |
| 146 | // Figure out the LHS type and other properties. |
| 147 | // |
Bram Moolenaar | 97f8c10 | 2022-04-02 19:43:57 +0100 | [diff] [blame] | 148 | ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, FALSE, 0, cctx); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 149 | |
Bram Moolenaar | 2ef01d9 | 2022-01-06 21:38:11 +0000 | [diff] [blame] | 150 | // Use the info in "lhs" to unlet the item at the index in the |
| 151 | // list or dict. |
| 152 | if (ret == OK) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 153 | { |
Bram Moolenaar | 2ef01d9 | 2022-01-06 21:38:11 +0000 | [diff] [blame] | 154 | if (!lhs.lhs_has_index) |
| 155 | { |
| 156 | semsg(_(e_cannot_unlet_imported_item_str), p); |
| 157 | ret = FAIL; |
| 158 | } |
| 159 | else |
| 160 | ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | vim_free(lhs.lhs_name); |
| 164 | } |
| 165 | else if (check_vim9_unlet(p) == FAIL) |
| 166 | { |
| 167 | ret = FAIL; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 172 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
| 173 | } |
| 174 | |
| 175 | *name_end = cc; |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Callback passed to ex_unletlock(). |
| 181 | */ |
| 182 | static int |
| 183 | compile_lock_unlock( |
| 184 | lval_T *lvp, |
| 185 | char_u *name_end, |
| 186 | exarg_T *eap, |
Bram Moolenaar | 70c43d8 | 2022-01-26 21:01:15 +0000 | [diff] [blame] | 187 | int deep, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 188 | void *coookie) |
| 189 | { |
| 190 | cctx_T *cctx = coookie; |
| 191 | int cc = *name_end; |
| 192 | char_u *p = lvp->ll_name; |
| 193 | int ret = OK; |
| 194 | size_t len; |
| 195 | char_u *buf; |
| 196 | isntype_T isn = ISN_EXEC; |
Bram Moolenaar | d1d8f6b | 2022-08-14 21:28:32 +0100 | [diff] [blame] | 197 | char *cmd = eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar"; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 198 | |
| 199 | if (cctx->ctx_skip == SKIP_YES) |
| 200 | return OK; |
| 201 | |
Bram Moolenaar | d1d8f6b | 2022-08-14 21:28:32 +0100 | [diff] [blame] | 202 | if (*p == NUL) |
| 203 | { |
| 204 | semsg(_(e_argument_required_for_str), cmd); |
| 205 | return FAIL; |
| 206 | } |
| 207 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 208 | // Cannot use :lockvar and :unlockvar on local variables. |
| 209 | if (p[1] != ':') |
| 210 | { |
| 211 | char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START); |
| 212 | |
| 213 | if (lookup_local(p, end - p, NULL, cctx) == OK) |
| 214 | { |
| 215 | char_u *s = p; |
| 216 | |
| 217 | if (*end != '.' && *end != '[') |
| 218 | { |
| 219 | emsg(_(e_cannot_lock_unlock_local_variable)); |
| 220 | return FAIL; |
| 221 | } |
| 222 | |
| 223 | // For "d.member" put the local variable on the stack, it will be |
| 224 | // passed to ex_lockvar() indirectly. |
| 225 | if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL) |
| 226 | return FAIL; |
| 227 | isn = ISN_LOCKUNLOCK; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // Checking is done at runtime. |
| 232 | *name_end = NUL; |
| 233 | len = name_end - p + 20; |
| 234 | buf = alloc(len); |
| 235 | if (buf == NULL) |
| 236 | ret = FAIL; |
| 237 | else |
| 238 | { |
Bram Moolenaar | e939f5e | 2022-01-26 21:32:59 +0000 | [diff] [blame] | 239 | if (deep < 0) |
| 240 | vim_snprintf((char *)buf, len, "%s! %s", cmd, p); |
| 241 | else |
| 242 | vim_snprintf((char *)buf, len, "%s %d %s", cmd, deep, p); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 243 | ret = generate_EXEC_copy(cctx, isn, buf); |
| 244 | |
| 245 | vim_free(buf); |
| 246 | *name_end = cc; |
| 247 | } |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * compile "unlet var", "lock var" and "unlock var" |
| 253 | * "arg" points to "var". |
| 254 | */ |
| 255 | char_u * |
| 256 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 257 | { |
Bram Moolenaar | 70c43d8 | 2022-01-26 21:01:15 +0000 | [diff] [blame] | 258 | int deep = 0; |
| 259 | char_u *p = arg; |
| 260 | |
| 261 | if (eap->cmdidx != CMD_unlet) |
| 262 | { |
| 263 | if (eap->forceit) |
| 264 | deep = -1; |
| 265 | else if (vim_isdigit(*p)) |
| 266 | { |
| 267 | deep = getdigits(&p); |
| 268 | p = skipwhite(p); |
| 269 | } |
| 270 | else |
| 271 | deep = 2; |
| 272 | } |
| 273 | |
| 274 | ex_unletlock(eap, p, deep, GLV_NO_AUTOLOAD | GLV_COMPILING, |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 275 | eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock, |
| 276 | cctx); |
| 277 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 282 | */ |
| 283 | static int |
| 284 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 285 | { |
| 286 | garray_T *instr = &cctx->ctx_instr; |
| 287 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 288 | |
| 289 | if (endlabel == NULL) |
| 290 | return FAIL; |
| 291 | endlabel->el_next = *el; |
| 292 | *el = endlabel; |
| 293 | endlabel->el_end_label = instr->ga_len; |
| 294 | |
| 295 | generate_JUMP(cctx, when, 0); |
| 296 | return OK; |
| 297 | } |
| 298 | |
| 299 | static void |
| 300 | compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx) |
| 301 | { |
| 302 | garray_T *instr = &cctx->ctx_instr; |
| 303 | |
| 304 | while (*el != NULL) |
| 305 | { |
| 306 | endlabel_T *cur = (*el); |
| 307 | isn_T *isn; |
| 308 | |
| 309 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 310 | isn->isn_arg.jump.jump_where = jump_where; |
| 311 | *el = cur->el_next; |
| 312 | vim_free(cur); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | static void |
| 317 | compile_free_jump_to_end(endlabel_T **el) |
| 318 | { |
| 319 | while (*el != NULL) |
| 320 | { |
| 321 | endlabel_T *cur = (*el); |
| 322 | |
| 323 | *el = cur->el_next; |
| 324 | vim_free(cur); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * Create a new scope and set up the generic items. |
| 330 | */ |
| 331 | static scope_T * |
| 332 | new_scope(cctx_T *cctx, scopetype_T type) |
| 333 | { |
| 334 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 335 | |
| 336 | if (scope == NULL) |
| 337 | return NULL; |
| 338 | scope->se_outer = cctx->ctx_scope; |
| 339 | cctx->ctx_scope = scope; |
| 340 | scope->se_type = type; |
| 341 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 342 | return scope; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Free the current scope and go back to the outer scope. |
| 347 | */ |
| 348 | void |
| 349 | drop_scope(cctx_T *cctx) |
| 350 | { |
| 351 | scope_T *scope = cctx->ctx_scope; |
| 352 | |
| 353 | if (scope == NULL) |
| 354 | { |
| 355 | iemsg("calling drop_scope() without a scope"); |
| 356 | return; |
| 357 | } |
| 358 | cctx->ctx_scope = scope->se_outer; |
| 359 | switch (scope->se_type) |
| 360 | { |
| 361 | case IF_SCOPE: |
| 362 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 363 | case FOR_SCOPE: |
| 364 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 365 | case WHILE_SCOPE: |
| 366 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 367 | case TRY_SCOPE: |
| 368 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 369 | case NO_SCOPE: |
| 370 | case BLOCK_SCOPE: |
| 371 | break; |
| 372 | } |
| 373 | vim_free(scope); |
| 374 | } |
| 375 | |
| 376 | static int |
| 377 | misplaced_cmdmod(cctx_T *cctx) |
| 378 | { |
| 379 | garray_T *instr = &cctx->ctx_instr; |
| 380 | |
| 381 | if (cctx->ctx_has_cmdmod |
| 382 | && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type |
| 383 | == ISN_CMDMOD) |
| 384 | { |
| 385 | emsg(_(e_misplaced_command_modifier)); |
| 386 | return TRUE; |
| 387 | } |
| 388 | return FALSE; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * compile "if expr" |
| 393 | * |
| 394 | * "if expr" Produces instructions: |
| 395 | * EVAL expr Push result of "expr" |
| 396 | * JUMP_IF_FALSE end |
| 397 | * ... body ... |
| 398 | * end: |
| 399 | * |
| 400 | * "if expr | else" Produces instructions: |
| 401 | * EVAL expr Push result of "expr" |
| 402 | * JUMP_IF_FALSE else |
| 403 | * ... body ... |
| 404 | * JUMP_ALWAYS end |
| 405 | * else: |
| 406 | * ... body ... |
| 407 | * end: |
| 408 | * |
| 409 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 410 | * EVAL expr Push result of "expr" |
| 411 | * JUMP_IF_FALSE elseif |
| 412 | * ... body ... |
| 413 | * JUMP_ALWAYS end |
| 414 | * elseif: |
| 415 | * EVAL expr Push result of "expr" |
| 416 | * JUMP_IF_FALSE else |
| 417 | * ... body ... |
| 418 | * JUMP_ALWAYS end |
| 419 | * else: |
| 420 | * ... body ... |
| 421 | * end: |
| 422 | */ |
| 423 | char_u * |
| 424 | compile_if(char_u *arg, cctx_T *cctx) |
| 425 | { |
| 426 | char_u *p = arg; |
| 427 | garray_T *instr = &cctx->ctx_instr; |
| 428 | int instr_count = instr->ga_len; |
| 429 | scope_T *scope; |
| 430 | skip_T skip_save = cctx->ctx_skip; |
| 431 | ppconst_T ppconst; |
| 432 | |
| 433 | CLEAR_FIELD(ppconst); |
| 434 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
| 435 | { |
| 436 | clear_ppconst(&ppconst); |
| 437 | return NULL; |
| 438 | } |
| 439 | if (!ends_excmd2(arg, skipwhite(p))) |
| 440 | { |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 441 | semsg(_(e_trailing_characters_str), p); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 442 | return NULL; |
| 443 | } |
| 444 | if (cctx->ctx_skip == SKIP_YES) |
| 445 | clear_ppconst(&ppconst); |
| 446 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
| 447 | { |
| 448 | int error = FALSE; |
| 449 | int v; |
| 450 | |
| 451 | // The expression results in a constant. |
| 452 | v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); |
| 453 | clear_ppconst(&ppconst); |
| 454 | if (error) |
| 455 | return NULL; |
| 456 | cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | // Not a constant, generate instructions for the expression. |
| 461 | cctx->ctx_skip = SKIP_UNKNOWN; |
| 462 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 463 | return NULL; |
| 464 | if (bool_on_stack(cctx) == FAIL) |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | // CMDMOD_REV must come before the jump |
| 469 | generate_undo_cmdmods(cctx); |
| 470 | |
| 471 | scope = new_scope(cctx, IF_SCOPE); |
| 472 | if (scope == NULL) |
| 473 | return NULL; |
| 474 | scope->se_skip_save = skip_save; |
| 475 | // "is_had_return" will be reset if any block does not end in :return |
| 476 | scope->se_u.se_if.is_had_return = TRUE; |
| 477 | |
| 478 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
| 479 | { |
| 480 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 481 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 482 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 483 | } |
| 484 | else |
| 485 | scope->se_u.se_if.is_if_label = -1; |
| 486 | |
| 487 | #ifdef FEAT_PROFILE |
| 488 | if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES |
| 489 | && skip_save != SKIP_YES) |
| 490 | { |
| 491 | // generated a profile start, need to generate a profile end, since it |
| 492 | // won't be done after returning |
| 493 | cctx->ctx_skip = SKIP_NOT; |
| 494 | generate_instr(cctx, ISN_PROF_END); |
| 495 | cctx->ctx_skip = SKIP_YES; |
| 496 | } |
| 497 | #endif |
| 498 | |
| 499 | return p; |
| 500 | } |
| 501 | |
| 502 | char_u * |
| 503 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 504 | { |
| 505 | char_u *p = arg; |
| 506 | garray_T *instr = &cctx->ctx_instr; |
| 507 | int instr_count; |
| 508 | isn_T *isn; |
| 509 | scope_T *scope = cctx->ctx_scope; |
| 510 | ppconst_T ppconst; |
| 511 | skip_T save_skip = cctx->ctx_skip; |
| 512 | |
| 513 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 514 | { |
| 515 | emsg(_(e_elseif_without_if)); |
| 516 | return NULL; |
| 517 | } |
| 518 | unwind_locals(cctx, scope->se_local_count); |
| 519 | if (!cctx->ctx_had_return) |
| 520 | scope->se_u.se_if.is_had_return = FALSE; |
| 521 | |
| 522 | if (cctx->ctx_skip == SKIP_NOT) |
| 523 | { |
| 524 | // previous block was executed, this one and following will not |
| 525 | cctx->ctx_skip = SKIP_YES; |
| 526 | scope->se_u.se_if.is_seen_skip_not = TRUE; |
| 527 | } |
| 528 | if (scope->se_u.se_if.is_seen_skip_not) |
| 529 | { |
| 530 | // A previous block was executed, skip over expression and bail out. |
| 531 | // Do not count the "elseif" for profiling and cmdmod |
| 532 | instr->ga_len = current_instr_idx(cctx); |
| 533 | |
| 534 | skip_expr_cctx(&p, cctx); |
| 535 | return p; |
| 536 | } |
| 537 | |
| 538 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
| 539 | { |
| 540 | int moved_cmdmod = FALSE; |
| 541 | int saved_debug = FALSE; |
| 542 | isn_T debug_isn; |
| 543 | |
| 544 | // Move any CMDMOD instruction to after the jump |
| 545 | if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD) |
| 546 | { |
| 547 | if (GA_GROW_FAILS(instr, 1)) |
| 548 | return NULL; |
| 549 | ((isn_T *)instr->ga_data)[instr->ga_len] = |
| 550 | ((isn_T *)instr->ga_data)[instr->ga_len - 1]; |
| 551 | --instr->ga_len; |
| 552 | moved_cmdmod = TRUE; |
| 553 | } |
| 554 | |
| 555 | // Remove the already generated ISN_DEBUG, it is written below the |
| 556 | // ISN_FOR instruction. |
| 557 | if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0 |
| 558 | && ((isn_T *)instr->ga_data)[instr->ga_len - 1] |
| 559 | .isn_type == ISN_DEBUG) |
| 560 | { |
| 561 | --instr->ga_len; |
| 562 | debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len]; |
| 563 | saved_debug = TRUE; |
| 564 | } |
| 565 | |
| 566 | if (compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 567 | JUMP_ALWAYS, cctx) == FAIL) |
| 568 | return NULL; |
| 569 | // previous "if" or "elseif" jumps here |
| 570 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 571 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 572 | |
| 573 | if (moved_cmdmod) |
| 574 | ++instr->ga_len; |
| 575 | |
| 576 | if (saved_debug) |
| 577 | { |
| 578 | // move the debug instruction here |
| 579 | if (GA_GROW_FAILS(instr, 1)) |
| 580 | return NULL; |
| 581 | ((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn; |
| 582 | ++instr->ga_len; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // compile "expr"; if we know it evaluates to FALSE skip the block |
| 587 | CLEAR_FIELD(ppconst); |
| 588 | if (cctx->ctx_skip == SKIP_YES) |
| 589 | { |
| 590 | cctx->ctx_skip = SKIP_UNKNOWN; |
| 591 | #ifdef FEAT_PROFILE |
| 592 | if (cctx->ctx_compile_type == CT_PROFILE) |
| 593 | // the previous block was skipped, need to profile this line |
| 594 | generate_instr(cctx, ISN_PROF_START); |
| 595 | #endif |
| 596 | if (cctx->ctx_compile_type == CT_DEBUG) |
| 597 | // the previous block was skipped, may want to debug this line |
| 598 | generate_instr_debug(cctx); |
| 599 | } |
| 600 | |
| 601 | instr_count = instr->ga_len; |
| 602 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
| 603 | { |
| 604 | clear_ppconst(&ppconst); |
| 605 | return NULL; |
| 606 | } |
| 607 | cctx->ctx_skip = save_skip; |
| 608 | if (!ends_excmd2(arg, skipwhite(p))) |
| 609 | { |
| 610 | clear_ppconst(&ppconst); |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 611 | semsg(_(e_trailing_characters_str), p); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 612 | return NULL; |
| 613 | } |
| 614 | if (scope->se_skip_save == SKIP_YES) |
| 615 | clear_ppconst(&ppconst); |
| 616 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
| 617 | { |
| 618 | int error = FALSE; |
| 619 | int v; |
| 620 | |
| 621 | // The expression result is a constant. |
| 622 | v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); |
| 623 | if (error) |
| 624 | { |
| 625 | clear_ppconst(&ppconst); |
| 626 | return NULL; |
| 627 | } |
| 628 | cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; |
| 629 | clear_ppconst(&ppconst); |
| 630 | scope->se_u.se_if.is_if_label = -1; |
| 631 | } |
| 632 | else |
| 633 | { |
| 634 | // Not a constant, generate instructions for the expression. |
| 635 | cctx->ctx_skip = SKIP_UNKNOWN; |
| 636 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 637 | return NULL; |
| 638 | if (bool_on_stack(cctx) == FAIL) |
| 639 | return NULL; |
| 640 | |
| 641 | // CMDMOD_REV must come before the jump |
| 642 | generate_undo_cmdmods(cctx); |
| 643 | |
| 644 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 645 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 646 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 647 | } |
| 648 | |
| 649 | return p; |
| 650 | } |
| 651 | |
| 652 | char_u * |
| 653 | compile_else(char_u *arg, cctx_T *cctx) |
| 654 | { |
| 655 | char_u *p = arg; |
| 656 | garray_T *instr = &cctx->ctx_instr; |
| 657 | isn_T *isn; |
| 658 | scope_T *scope = cctx->ctx_scope; |
| 659 | |
| 660 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 661 | { |
| 662 | emsg(_(e_else_without_if)); |
| 663 | return NULL; |
| 664 | } |
| 665 | unwind_locals(cctx, scope->se_local_count); |
| 666 | if (!cctx->ctx_had_return) |
| 667 | scope->se_u.se_if.is_had_return = FALSE; |
| 668 | scope->se_u.se_if.is_seen_else = TRUE; |
| 669 | |
| 670 | #ifdef FEAT_PROFILE |
| 671 | if (cctx->ctx_compile_type == CT_PROFILE) |
| 672 | { |
| 673 | if (cctx->ctx_skip == SKIP_NOT |
| 674 | && ((isn_T *)instr->ga_data)[instr->ga_len - 1] |
| 675 | .isn_type == ISN_PROF_START) |
| 676 | // the previous block was executed, do not count "else" for |
| 677 | // profiling |
| 678 | --instr->ga_len; |
| 679 | if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not) |
| 680 | { |
| 681 | // the previous block was not executed, this one will, do count the |
| 682 | // "else" for profiling |
| 683 | cctx->ctx_skip = SKIP_NOT; |
| 684 | generate_instr(cctx, ISN_PROF_END); |
| 685 | generate_instr(cctx, ISN_PROF_START); |
| 686 | cctx->ctx_skip = SKIP_YES; |
| 687 | } |
| 688 | } |
| 689 | #endif |
| 690 | |
| 691 | if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES) |
| 692 | { |
| 693 | // jump from previous block to the end, unless the else block is empty |
| 694 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
| 695 | { |
| 696 | if (!cctx->ctx_had_return |
| 697 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 698 | JUMP_ALWAYS, cctx) == FAIL) |
| 699 | return NULL; |
| 700 | } |
| 701 | |
| 702 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
| 703 | { |
| 704 | if (scope->se_u.se_if.is_if_label >= 0) |
| 705 | { |
| 706 | // previous "if" or "elseif" jumps here |
| 707 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 708 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 709 | scope->se_u.se_if.is_if_label = -1; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
| 714 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 715 | } |
| 716 | |
| 717 | return p; |
| 718 | } |
| 719 | |
| 720 | char_u * |
| 721 | compile_endif(char_u *arg, cctx_T *cctx) |
| 722 | { |
| 723 | scope_T *scope = cctx->ctx_scope; |
| 724 | ifscope_T *ifscope; |
| 725 | garray_T *instr = &cctx->ctx_instr; |
| 726 | isn_T *isn; |
| 727 | |
| 728 | if (misplaced_cmdmod(cctx)) |
| 729 | return NULL; |
| 730 | |
| 731 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 732 | { |
| 733 | emsg(_(e_endif_without_if)); |
| 734 | return NULL; |
| 735 | } |
| 736 | ifscope = &scope->se_u.se_if; |
| 737 | unwind_locals(cctx, scope->se_local_count); |
| 738 | if (!cctx->ctx_had_return) |
| 739 | ifscope->is_had_return = FALSE; |
| 740 | |
| 741 | if (scope->se_u.se_if.is_if_label >= 0) |
| 742 | { |
| 743 | // previous "if" or "elseif" jumps here |
| 744 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 745 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 746 | } |
| 747 | // Fill in the "end" label in jumps at the end of the blocks. |
| 748 | compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx); |
| 749 | |
| 750 | #ifdef FEAT_PROFILE |
| 751 | // even when skipping we count the endif as executed, unless the block it's |
| 752 | // in is skipped |
| 753 | if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES |
| 754 | && scope->se_skip_save != SKIP_YES) |
| 755 | { |
| 756 | cctx->ctx_skip = SKIP_NOT; |
| 757 | generate_instr(cctx, ISN_PROF_START); |
| 758 | } |
| 759 | #endif |
| 760 | cctx->ctx_skip = scope->se_skip_save; |
| 761 | |
| 762 | // If all the blocks end in :return and there is an :else then the |
| 763 | // had_return flag is set. |
| 764 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
| 765 | |
| 766 | drop_scope(cctx); |
| 767 | return arg; |
| 768 | } |
| 769 | |
| 770 | /* |
| 771 | * Compile "for var in expr": |
| 772 | * |
| 773 | * Produces instructions: |
| 774 | * PUSHNR -1 |
| 775 | * STORE loop-idx Set index to -1 |
| 776 | * EVAL expr result of "expr" on top of stack |
| 777 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 778 | * - if beyond end, jump to "end" |
| 779 | * - otherwise get item from list and push it |
| 780 | * STORE var Store item in "var" |
| 781 | * ... body ... |
| 782 | * JUMP top Jump back to repeat |
| 783 | * end: DROP Drop the result of "expr" |
| 784 | * |
| 785 | * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var": |
| 786 | * UNPACK 2 Split item in 2 |
| 787 | * STORE var1 Store item in "var1" |
| 788 | * STORE var2 Store item in "var2" |
| 789 | */ |
| 790 | char_u * |
| 791 | compile_for(char_u *arg_start, cctx_T *cctx) |
| 792 | { |
| 793 | char_u *arg; |
| 794 | char_u *arg_end; |
| 795 | char_u *name = NULL; |
| 796 | char_u *p; |
| 797 | char_u *wp; |
| 798 | int var_count = 0; |
| 799 | int var_list = FALSE; |
| 800 | int semicolon = FALSE; |
| 801 | size_t varlen; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 802 | garray_T *instr = &cctx->ctx_instr; |
| 803 | scope_T *scope; |
| 804 | lvar_T *loop_lvar; // loop iteration variable |
| 805 | lvar_T *var_lvar; // variable for "var" |
| 806 | type_T *vartype; |
| 807 | type_T *item_type = &t_any; |
| 808 | int idx; |
| 809 | int prev_lnum = cctx->ctx_prev_lnum; |
| 810 | |
| 811 | p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE); |
| 812 | if (p == NULL) |
| 813 | return NULL; |
| 814 | if (var_count == 0) |
| 815 | var_count = 1; |
| 816 | else |
| 817 | var_list = TRUE; // can also be a list of one variable |
| 818 | |
| 819 | // consume "in" |
| 820 | wp = p; |
| 821 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
| 822 | return NULL; |
| 823 | if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2])) |
| 824 | { |
| 825 | if (*p == ':' && wp != p) |
| 826 | semsg(_(e_no_white_space_allowed_before_colon_str), p); |
| 827 | else |
Bram Moolenaar | 3a846e6 | 2022-01-01 16:21:00 +0000 | [diff] [blame] | 828 | emsg(_(e_missing_in_after_for)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 829 | return NULL; |
| 830 | } |
| 831 | wp = p + 2; |
| 832 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
| 833 | return NULL; |
| 834 | |
Bram Moolenaar | 2b4ecc2 | 2022-01-02 14:08:18 +0000 | [diff] [blame] | 835 | // Find the already generated ISN_DEBUG to get the line number for the |
| 836 | // instruction written below the ISN_FOR instruction. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 837 | if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0 |
| 838 | && ((isn_T *)instr->ga_data)[instr->ga_len - 1] |
| 839 | .isn_type == ISN_DEBUG) |
| 840 | { |
Bram Moolenaar | 2b4ecc2 | 2022-01-02 14:08:18 +0000 | [diff] [blame] | 841 | prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len - 1] |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 842 | .isn_arg.debug.dbg_break_lnum; |
| 843 | } |
| 844 | |
| 845 | scope = new_scope(cctx, FOR_SCOPE); |
| 846 | if (scope == NULL) |
| 847 | return NULL; |
| 848 | |
| 849 | // Reserve a variable to store the loop iteration counter and initialize it |
| 850 | // to -1. |
| 851 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 852 | if (loop_lvar == NULL) |
| 853 | { |
| 854 | // out of memory |
| 855 | drop_scope(cctx); |
| 856 | return NULL; |
| 857 | } |
| 858 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
| 859 | |
| 860 | // compile "expr", it remains on the stack until "endfor" |
| 861 | arg = p; |
| 862 | if (compile_expr0(&arg, cctx) == FAIL) |
| 863 | { |
| 864 | drop_scope(cctx); |
| 865 | return NULL; |
| 866 | } |
| 867 | arg_end = arg; |
| 868 | |
| 869 | if (cctx->ctx_skip != SKIP_YES) |
| 870 | { |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 871 | // If we know the type of "var" and it is not a supported type we can |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 872 | // give an error now. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 873 | vartype = get_type_on_stack(cctx, 0); |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 874 | if (vartype->tt_type != VAR_LIST |
| 875 | && vartype->tt_type != VAR_STRING |
| 876 | && vartype->tt_type != VAR_BLOB |
| 877 | && vartype->tt_type != VAR_ANY |
| 878 | && vartype->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 879 | { |
| 880 | semsg(_(e_for_loop_on_str_not_supported), |
| 881 | vartype_name(vartype->tt_type)); |
| 882 | drop_scope(cctx); |
| 883 | return NULL; |
| 884 | } |
| 885 | |
| 886 | if (vartype->tt_type == VAR_STRING) |
| 887 | item_type = &t_string; |
| 888 | else if (vartype->tt_type == VAR_BLOB) |
| 889 | item_type = &t_number; |
| 890 | else if (vartype->tt_type == VAR_LIST |
| 891 | && vartype->tt_member->tt_type != VAR_ANY) |
| 892 | { |
| 893 | if (!var_list) |
| 894 | item_type = vartype->tt_member; |
| 895 | else if (vartype->tt_member->tt_type == VAR_LIST |
| 896 | && vartype->tt_member->tt_member->tt_type != VAR_ANY) |
| 897 | item_type = vartype->tt_member->tt_member; |
| 898 | } |
| 899 | |
| 900 | // CMDMOD_REV must come before the FOR instruction. |
| 901 | generate_undo_cmdmods(cctx); |
| 902 | |
| 903 | // "for_end" is set when ":endfor" is found |
| 904 | scope->se_u.se_for.fs_top_label = current_instr_idx(cctx); |
| 905 | |
Bram Moolenaar | 2b4ecc2 | 2022-01-02 14:08:18 +0000 | [diff] [blame] | 906 | if (cctx->ctx_compile_type == CT_DEBUG) |
| 907 | { |
| 908 | int save_prev_lnum = cctx->ctx_prev_lnum; |
| 909 | isn_T *isn; |
| 910 | |
| 911 | // Add ISN_DEBUG here, before deciding to end the loop. There will |
| 912 | // be another ISN_DEBUG before the next instruction. |
| 913 | // Use the prev_lnum from the ISN_DEBUG instruction removed above. |
| 914 | // Increment the variable count so that the loop variable can be |
| 915 | // inspected. |
| 916 | cctx->ctx_prev_lnum = prev_lnum; |
| 917 | isn = generate_instr_debug(cctx); |
| 918 | ++isn->isn_arg.debug.dbg_var_names_len; |
| 919 | cctx->ctx_prev_lnum = save_prev_lnum; |
| 920 | } |
| 921 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 922 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 923 | |
| 924 | arg = arg_start; |
| 925 | if (var_list) |
| 926 | { |
| 927 | generate_UNPACK(cctx, var_count, semicolon); |
| 928 | arg = skipwhite(arg + 1); // skip white after '[' |
| 929 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 930 | // drop the list item |
| 931 | --cctx->ctx_type_stack.ga_len; |
| 932 | |
| 933 | // add type of the items |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 934 | for (idx = 0; idx < var_count; ++idx) |
| 935 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 936 | type_T *type = (semicolon && idx == 0) ? vartype : item_type; |
| 937 | |
| 938 | if (push_type_stack(cctx, type) == FAIL) |
| 939 | { |
| 940 | drop_scope(cctx); |
| 941 | return NULL; |
| 942 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | |
| 946 | for (idx = 0; idx < var_count; ++idx) |
| 947 | { |
| 948 | assign_dest_T dest = dest_local; |
| 949 | int opt_flags = 0; |
| 950 | int vimvaridx = -1; |
| 951 | type_T *type = &t_any; |
| 952 | type_T *lhs_type = &t_any; |
| 953 | where_T where = WHERE_INIT; |
| 954 | |
| 955 | p = skip_var_one(arg, FALSE); |
| 956 | varlen = p - arg; |
| 957 | name = vim_strnsave(arg, varlen); |
| 958 | if (name == NULL) |
| 959 | goto failed; |
| 960 | if (*p == ':') |
| 961 | { |
| 962 | p = skipwhite(p + 1); |
| 963 | lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE); |
| 964 | } |
| 965 | |
| 966 | if (get_var_dest(name, &dest, CMD_for, &opt_flags, |
| 967 | &vimvaridx, &type, cctx) == FAIL) |
| 968 | goto failed; |
| 969 | if (dest != dest_local) |
| 970 | { |
| 971 | if (generate_store_var(cctx, dest, opt_flags, vimvaridx, |
| 972 | 0, 0, type, name) == FAIL) |
| 973 | goto failed; |
| 974 | } |
| 975 | else if (varlen == 1 && *arg == '_') |
| 976 | { |
| 977 | // Assigning to "_": drop the value. |
| 978 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 979 | goto failed; |
| 980 | } |
| 981 | else |
| 982 | { |
| 983 | // Script var is not supported. |
| 984 | if (STRNCMP(name, "s:", 2) == 0) |
| 985 | { |
| 986 | emsg(_(e_cannot_use_script_variable_in_for_loop)); |
| 987 | goto failed; |
| 988 | } |
| 989 | |
| 990 | if (!valid_varname(arg, (int)varlen, FALSE)) |
| 991 | goto failed; |
| 992 | if (lookup_local(arg, varlen, NULL, cctx) == OK) |
| 993 | { |
| 994 | semsg(_(e_variable_already_declared), arg); |
| 995 | goto failed; |
| 996 | } |
| 997 | |
| 998 | // Reserve a variable to store "var". |
| 999 | where.wt_index = var_list ? idx + 1 : 0; |
| 1000 | where.wt_variable = TRUE; |
| 1001 | if (lhs_type == &t_any) |
| 1002 | lhs_type = item_type; |
| 1003 | else if (item_type != &t_unknown |
Bram Moolenaar | a1c5195 | 2022-02-02 16:20:26 +0000 | [diff] [blame] | 1004 | && need_type_where(item_type, lhs_type, -1, |
| 1005 | where, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1006 | goto failed; |
| 1007 | var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type); |
| 1008 | if (var_lvar == NULL) |
| 1009 | // out of memory or used as an argument |
| 1010 | goto failed; |
| 1011 | |
| 1012 | if (semicolon && idx == var_count - 1) |
| 1013 | var_lvar->lv_type = vartype; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1014 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
| 1015 | } |
| 1016 | |
| 1017 | if (*p == ',' || *p == ';') |
| 1018 | ++p; |
| 1019 | arg = skipwhite(p); |
| 1020 | vim_free(name); |
| 1021 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | return arg_end; |
| 1025 | |
| 1026 | failed: |
| 1027 | vim_free(name); |
| 1028 | drop_scope(cctx); |
| 1029 | return NULL; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * compile "endfor" |
| 1034 | */ |
| 1035 | char_u * |
| 1036 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 1037 | { |
| 1038 | garray_T *instr = &cctx->ctx_instr; |
| 1039 | scope_T *scope = cctx->ctx_scope; |
| 1040 | forscope_T *forscope; |
| 1041 | isn_T *isn; |
| 1042 | |
| 1043 | if (misplaced_cmdmod(cctx)) |
| 1044 | return NULL; |
| 1045 | |
| 1046 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 1047 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1048 | emsg(_(e_endfor_without_for)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1049 | return NULL; |
| 1050 | } |
| 1051 | forscope = &scope->se_u.se_for; |
| 1052 | cctx->ctx_scope = scope->se_outer; |
| 1053 | if (cctx->ctx_skip != SKIP_YES) |
| 1054 | { |
| 1055 | unwind_locals(cctx, scope->se_local_count); |
| 1056 | |
| 1057 | // At end of ":for" scope jump back to the FOR instruction. |
| 1058 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 1059 | |
| 1060 | // Fill in the "end" label in the FOR statement so it can jump here. |
Bram Moolenaar | 2b4ecc2 | 2022-01-02 14:08:18 +0000 | [diff] [blame] | 1061 | // In debug mode an ISN_DEBUG was inserted. |
| 1062 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label |
| 1063 | + (cctx->ctx_compile_type == CT_DEBUG ? 1 : 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1064 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 1065 | |
| 1066 | // Fill in the "end" label any BREAK statements |
| 1067 | compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx); |
| 1068 | |
| 1069 | // Below the ":for" scope drop the "expr" list from the stack. |
| 1070 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 1071 | return NULL; |
| 1072 | } |
| 1073 | |
| 1074 | vim_free(scope); |
| 1075 | |
| 1076 | return arg; |
| 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * compile "while expr" |
| 1081 | * |
| 1082 | * Produces instructions: |
| 1083 | * top: EVAL expr Push result of "expr" |
| 1084 | * JUMP_IF_FALSE end jump if false |
| 1085 | * ... body ... |
| 1086 | * JUMP top Jump back to repeat |
| 1087 | * end: |
| 1088 | * |
| 1089 | */ |
| 1090 | char_u * |
| 1091 | compile_while(char_u *arg, cctx_T *cctx) |
| 1092 | { |
| 1093 | char_u *p = arg; |
| 1094 | scope_T *scope; |
| 1095 | |
| 1096 | scope = new_scope(cctx, WHILE_SCOPE); |
| 1097 | if (scope == NULL) |
| 1098 | return NULL; |
| 1099 | |
| 1100 | // "endwhile" jumps back here, one before when profiling or using cmdmods |
| 1101 | scope->se_u.se_while.ws_top_label = current_instr_idx(cctx); |
| 1102 | |
| 1103 | // compile "expr" |
| 1104 | if (compile_expr0(&p, cctx) == FAIL) |
| 1105 | return NULL; |
| 1106 | |
| 1107 | if (!ends_excmd2(arg, skipwhite(p))) |
| 1108 | { |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 1109 | semsg(_(e_trailing_characters_str), p); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1110 | return NULL; |
| 1111 | } |
| 1112 | |
| 1113 | if (cctx->ctx_skip != SKIP_YES) |
| 1114 | { |
| 1115 | if (bool_on_stack(cctx) == FAIL) |
| 1116 | return FAIL; |
| 1117 | |
| 1118 | // CMDMOD_REV must come before the jump |
| 1119 | generate_undo_cmdmods(cctx); |
| 1120 | |
| 1121 | // "while_end" is set when ":endwhile" is found |
| 1122 | if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label, |
| 1123 | JUMP_IF_FALSE, cctx) == FAIL) |
| 1124 | return FAIL; |
| 1125 | } |
| 1126 | |
| 1127 | return p; |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * compile "endwhile" |
| 1132 | */ |
| 1133 | char_u * |
| 1134 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 1135 | { |
| 1136 | scope_T *scope = cctx->ctx_scope; |
| 1137 | garray_T *instr = &cctx->ctx_instr; |
| 1138 | |
| 1139 | if (misplaced_cmdmod(cctx)) |
| 1140 | return NULL; |
| 1141 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 1142 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1143 | emsg(_(e_endwhile_without_while)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1144 | return NULL; |
| 1145 | } |
| 1146 | cctx->ctx_scope = scope->se_outer; |
| 1147 | if (cctx->ctx_skip != SKIP_YES) |
| 1148 | { |
| 1149 | unwind_locals(cctx, scope->se_local_count); |
| 1150 | |
| 1151 | #ifdef FEAT_PROFILE |
| 1152 | // count the endwhile before jumping |
| 1153 | may_generate_prof_end(cctx, cctx->ctx_lnum); |
| 1154 | #endif |
| 1155 | |
| 1156 | // At end of ":for" scope jump back to the FOR instruction. |
| 1157 | generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label); |
| 1158 | |
| 1159 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 1160 | // And in any jumps for ":break" |
| 1161 | compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, |
| 1162 | instr->ga_len, cctx); |
| 1163 | } |
| 1164 | |
| 1165 | vim_free(scope); |
| 1166 | |
| 1167 | return arg; |
| 1168 | } |
| 1169 | |
| 1170 | /* |
| 1171 | * compile "continue" |
| 1172 | */ |
| 1173 | char_u * |
| 1174 | compile_continue(char_u *arg, cctx_T *cctx) |
| 1175 | { |
| 1176 | scope_T *scope = cctx->ctx_scope; |
| 1177 | int try_scopes = 0; |
| 1178 | int loop_label; |
| 1179 | |
| 1180 | for (;;) |
| 1181 | { |
| 1182 | if (scope == NULL) |
| 1183 | { |
Bram Moolenaar | 3a846e6 | 2022-01-01 16:21:00 +0000 | [diff] [blame] | 1184 | emsg(_(e_continue_without_while_or_for)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1185 | return NULL; |
| 1186 | } |
| 1187 | if (scope->se_type == FOR_SCOPE) |
| 1188 | { |
| 1189 | loop_label = scope->se_u.se_for.fs_top_label; |
| 1190 | break; |
| 1191 | } |
| 1192 | if (scope->se_type == WHILE_SCOPE) |
| 1193 | { |
| 1194 | loop_label = scope->se_u.se_while.ws_top_label; |
| 1195 | break; |
| 1196 | } |
| 1197 | if (scope->se_type == TRY_SCOPE) |
| 1198 | ++try_scopes; |
| 1199 | scope = scope->se_outer; |
| 1200 | } |
| 1201 | |
| 1202 | if (try_scopes > 0) |
| 1203 | // Inside one or more try/catch blocks we first need to jump to the |
| 1204 | // "finally" or "endtry" to cleanup. |
| 1205 | generate_TRYCONT(cctx, try_scopes, loop_label); |
| 1206 | else |
| 1207 | // Jump back to the FOR or WHILE instruction. |
| 1208 | generate_JUMP(cctx, JUMP_ALWAYS, loop_label); |
| 1209 | |
| 1210 | return arg; |
| 1211 | } |
| 1212 | |
| 1213 | /* |
| 1214 | * compile "break" |
| 1215 | */ |
| 1216 | char_u * |
| 1217 | compile_break(char_u *arg, cctx_T *cctx) |
| 1218 | { |
| 1219 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 873f824 | 2022-03-10 21:53:44 +0000 | [diff] [blame] | 1220 | int try_scopes = 0; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1221 | endlabel_T **el; |
| 1222 | |
| 1223 | for (;;) |
| 1224 | { |
| 1225 | if (scope == NULL) |
| 1226 | { |
Bram Moolenaar | 3a846e6 | 2022-01-01 16:21:00 +0000 | [diff] [blame] | 1227 | emsg(_(e_break_without_while_or_for)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1228 | return NULL; |
| 1229 | } |
Bram Moolenaar | 873f824 | 2022-03-10 21:53:44 +0000 | [diff] [blame] | 1230 | if (scope->se_type == FOR_SCOPE) |
| 1231 | { |
| 1232 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1233 | break; |
Bram Moolenaar | 873f824 | 2022-03-10 21:53:44 +0000 | [diff] [blame] | 1234 | } |
| 1235 | if (scope->se_type == WHILE_SCOPE) |
| 1236 | { |
| 1237 | el = &scope->se_u.se_while.ws_end_label; |
| 1238 | break; |
| 1239 | } |
| 1240 | if (scope->se_type == TRY_SCOPE) |
| 1241 | ++try_scopes; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1242 | scope = scope->se_outer; |
| 1243 | } |
| 1244 | |
Bram Moolenaar | 873f824 | 2022-03-10 21:53:44 +0000 | [diff] [blame] | 1245 | if (try_scopes > 0) |
| 1246 | // Inside one or more try/catch blocks we first need to jump to the |
| 1247 | // "finally" or "endtry" to cleanup. Then come to the next JUMP |
| 1248 | // intruction, which we don't know the index of yet. |
| 1249 | generate_TRYCONT(cctx, try_scopes, cctx->ctx_instr.ga_len + 1); |
| 1250 | |
| 1251 | // Jump to the end of the FOR or WHILE loop. The instruction index will be |
| 1252 | // filled in later. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1253 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 1254 | return FAIL; |
| 1255 | |
| 1256 | return arg; |
| 1257 | } |
| 1258 | |
| 1259 | /* |
| 1260 | * compile "{" start of block |
| 1261 | */ |
| 1262 | char_u * |
| 1263 | compile_block(char_u *arg, cctx_T *cctx) |
| 1264 | { |
| 1265 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 1266 | return NULL; |
| 1267 | return skipwhite(arg + 1); |
| 1268 | } |
| 1269 | |
| 1270 | /* |
| 1271 | * compile end of block: drop one scope |
| 1272 | */ |
| 1273 | void |
| 1274 | compile_endblock(cctx_T *cctx) |
| 1275 | { |
| 1276 | scope_T *scope = cctx->ctx_scope; |
| 1277 | |
| 1278 | cctx->ctx_scope = scope->se_outer; |
| 1279 | unwind_locals(cctx, scope->se_local_count); |
| 1280 | vim_free(scope); |
| 1281 | } |
| 1282 | |
| 1283 | /* |
| 1284 | * Compile "try". |
| 1285 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 1286 | * finally. |
| 1287 | * Creates another scope for the "try" block itself. |
| 1288 | * TRY instruction sets up exception handling at runtime. |
| 1289 | * |
| 1290 | * "try" |
| 1291 | * TRY -> catch1, -> finally push trystack entry |
| 1292 | * ... try block |
| 1293 | * "throw {exception}" |
| 1294 | * EVAL {exception} |
| 1295 | * THROW create exception |
| 1296 | * ... try block |
| 1297 | * " catch {expr}" |
| 1298 | * JUMP -> finally |
| 1299 | * catch1: PUSH exception |
| 1300 | * EVAL {expr} |
| 1301 | * MATCH |
| 1302 | * JUMP nomatch -> catch2 |
| 1303 | * CATCH remove exception |
| 1304 | * ... catch block |
| 1305 | * " catch" |
| 1306 | * JUMP -> finally |
| 1307 | * catch2: CATCH remove exception |
| 1308 | * ... catch block |
| 1309 | * " finally" |
| 1310 | * finally: |
| 1311 | * ... finally block |
| 1312 | * " endtry" |
| 1313 | * ENDTRY pop trystack entry, may rethrow |
| 1314 | */ |
| 1315 | char_u * |
| 1316 | compile_try(char_u *arg, cctx_T *cctx) |
| 1317 | { |
| 1318 | garray_T *instr = &cctx->ctx_instr; |
| 1319 | scope_T *try_scope; |
| 1320 | scope_T *scope; |
| 1321 | |
| 1322 | if (misplaced_cmdmod(cctx)) |
| 1323 | return NULL; |
| 1324 | |
| 1325 | // scope that holds the jumps that go to catch/finally/endtry |
| 1326 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 1327 | if (try_scope == NULL) |
| 1328 | return NULL; |
| 1329 | |
| 1330 | if (cctx->ctx_skip != SKIP_YES) |
| 1331 | { |
| 1332 | isn_T *isn; |
| 1333 | |
| 1334 | // "try_catch" is set when the first ":catch" is found or when no catch |
| 1335 | // is found and ":finally" is found. |
| 1336 | // "try_finally" is set when ":finally" is found |
| 1337 | // "try_endtry" is set when ":endtry" is found |
| 1338 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
| 1339 | if ((isn = generate_instr(cctx, ISN_TRY)) == NULL) |
| 1340 | return NULL; |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 1341 | isn->isn_arg.tryref.try_ref = ALLOC_CLEAR_ONE(tryref_T); |
| 1342 | if (isn->isn_arg.tryref.try_ref == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1343 | return NULL; |
| 1344 | } |
| 1345 | |
| 1346 | // scope for the try block itself |
| 1347 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 1348 | if (scope == NULL) |
| 1349 | return NULL; |
| 1350 | |
| 1351 | return arg; |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * Compile "catch {expr}". |
| 1356 | */ |
| 1357 | char_u * |
| 1358 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 1359 | { |
| 1360 | scope_T *scope = cctx->ctx_scope; |
| 1361 | garray_T *instr = &cctx->ctx_instr; |
| 1362 | char_u *p; |
| 1363 | isn_T *isn; |
| 1364 | |
| 1365 | if (misplaced_cmdmod(cctx)) |
| 1366 | return NULL; |
| 1367 | |
| 1368 | // end block scope from :try or :catch |
| 1369 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 1370 | compile_endblock(cctx); |
| 1371 | scope = cctx->ctx_scope; |
| 1372 | |
| 1373 | // Error if not in a :try scope |
| 1374 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 1375 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1376 | emsg(_(e_catch_without_try)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1377 | return NULL; |
| 1378 | } |
| 1379 | |
| 1380 | if (scope->se_u.se_try.ts_caught_all) |
| 1381 | { |
| 1382 | emsg(_(e_catch_unreachable_after_catch_all)); |
| 1383 | return NULL; |
| 1384 | } |
Bram Moolenaar | 53c2961 | 2022-01-12 16:18:18 +0000 | [diff] [blame] | 1385 | if (!cctx->ctx_had_return) |
| 1386 | scope->se_u.se_try.ts_no_return = TRUE; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1387 | |
| 1388 | if (cctx->ctx_skip != SKIP_YES) |
| 1389 | { |
| 1390 | #ifdef FEAT_PROFILE |
| 1391 | // the profile-start should be after the jump |
| 1392 | if (cctx->ctx_compile_type == CT_PROFILE |
| 1393 | && instr->ga_len > 0 |
| 1394 | && ((isn_T *)instr->ga_data)[instr->ga_len - 1] |
| 1395 | .isn_type == ISN_PROF_START) |
| 1396 | --instr->ga_len; |
| 1397 | #endif |
| 1398 | // Jump from end of previous block to :finally or :endtry |
| 1399 | if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label, |
| 1400 | JUMP_ALWAYS, cctx) == FAIL) |
| 1401 | return NULL; |
| 1402 | |
| 1403 | // End :try or :catch scope: set value in ISN_TRY instruction |
| 1404 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 1405 | if (isn->isn_arg.tryref.try_ref->try_catch == 0) |
| 1406 | isn->isn_arg.tryref.try_ref->try_catch = instr->ga_len; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1407 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 1408 | { |
| 1409 | // Previous catch without match jumps here |
| 1410 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 1411 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 1412 | } |
| 1413 | #ifdef FEAT_PROFILE |
| 1414 | if (cctx->ctx_compile_type == CT_PROFILE) |
| 1415 | { |
| 1416 | // a "throw" that jumps here needs to be counted |
| 1417 | generate_instr(cctx, ISN_PROF_END); |
| 1418 | // the "catch" is also counted |
| 1419 | generate_instr(cctx, ISN_PROF_START); |
| 1420 | } |
| 1421 | #endif |
| 1422 | if (cctx->ctx_compile_type == CT_DEBUG) |
| 1423 | generate_instr_debug(cctx); |
| 1424 | } |
| 1425 | |
| 1426 | p = skipwhite(arg); |
| 1427 | if (ends_excmd2(arg, p)) |
| 1428 | { |
| 1429 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 1430 | scope->se_u.se_try.ts_catch_label = 0; |
| 1431 | } |
| 1432 | else |
| 1433 | { |
| 1434 | char_u *end; |
| 1435 | char_u *pat; |
| 1436 | char_u *tofree = NULL; |
| 1437 | int dropped = 0; |
| 1438 | int len; |
| 1439 | |
| 1440 | // Push v:exception, push {expr} and MATCH |
| 1441 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 1442 | |
| 1443 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL); |
| 1444 | if (*end != *p) |
| 1445 | { |
| 1446 | semsg(_(e_separator_mismatch_str), p); |
| 1447 | vim_free(tofree); |
Bram Moolenaar | 54969f4 | 2022-02-07 13:56:44 +0000 | [diff] [blame] | 1448 | return NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1449 | } |
| 1450 | if (tofree == NULL) |
| 1451 | len = (int)(end - (p + 1)); |
| 1452 | else |
| 1453 | len = (int)(end - tofree); |
| 1454 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
| 1455 | vim_free(tofree); |
| 1456 | p += len + 2 + dropped; |
| 1457 | if (pat == NULL) |
Bram Moolenaar | 54969f4 | 2022-02-07 13:56:44 +0000 | [diff] [blame] | 1458 | return NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1459 | if (generate_PUSHS(cctx, &pat) == FAIL) |
Bram Moolenaar | 54969f4 | 2022-02-07 13:56:44 +0000 | [diff] [blame] | 1460 | return NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1461 | |
| 1462 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 1463 | return NULL; |
| 1464 | |
| 1465 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
| 1466 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 1467 | return NULL; |
| 1468 | } |
| 1469 | |
| 1470 | if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL) |
| 1471 | return NULL; |
| 1472 | |
| 1473 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 1474 | return NULL; |
| 1475 | return p; |
| 1476 | } |
| 1477 | |
| 1478 | char_u * |
| 1479 | compile_finally(char_u *arg, cctx_T *cctx) |
| 1480 | { |
| 1481 | scope_T *scope = cctx->ctx_scope; |
| 1482 | garray_T *instr = &cctx->ctx_instr; |
| 1483 | isn_T *isn; |
| 1484 | int this_instr; |
| 1485 | |
| 1486 | if (misplaced_cmdmod(cctx)) |
| 1487 | return NULL; |
| 1488 | |
| 1489 | // end block scope from :try or :catch |
| 1490 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 1491 | compile_endblock(cctx); |
| 1492 | scope = cctx->ctx_scope; |
| 1493 | |
| 1494 | // Error if not in a :try scope |
| 1495 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 1496 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1497 | emsg(_(e_finally_without_try)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1498 | return NULL; |
| 1499 | } |
| 1500 | |
| 1501 | if (cctx->ctx_skip != SKIP_YES) |
| 1502 | { |
| 1503 | // End :catch or :finally scope: set value in ISN_TRY instruction |
| 1504 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 1505 | if (isn->isn_arg.tryref.try_ref->try_finally != 0) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1506 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1507 | emsg(_(e_multiple_finally)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1508 | return NULL; |
| 1509 | } |
| 1510 | |
| 1511 | this_instr = instr->ga_len; |
| 1512 | #ifdef FEAT_PROFILE |
| 1513 | if (cctx->ctx_compile_type == CT_PROFILE |
| 1514 | && ((isn_T *)instr->ga_data)[this_instr - 1] |
| 1515 | .isn_type == ISN_PROF_START) |
| 1516 | { |
| 1517 | // jump to the profile start of the "finally" |
| 1518 | --this_instr; |
| 1519 | |
| 1520 | // jump to the profile end above it |
| 1521 | if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1] |
| 1522 | .isn_type == ISN_PROF_END) |
| 1523 | --this_instr; |
| 1524 | } |
| 1525 | #endif |
| 1526 | |
| 1527 | // Fill in the "end" label in jumps at the end of the blocks. |
| 1528 | compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, |
| 1529 | this_instr, cctx); |
| 1530 | |
| 1531 | // If there is no :catch then an exception jumps to :finally. |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 1532 | if (isn->isn_arg.tryref.try_ref->try_catch == 0) |
| 1533 | isn->isn_arg.tryref.try_ref->try_catch = this_instr; |
| 1534 | isn->isn_arg.tryref.try_ref->try_finally = this_instr; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1535 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 1536 | { |
| 1537 | // Previous catch without match jumps here |
| 1538 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 1539 | isn->isn_arg.jump.jump_where = this_instr; |
| 1540 | scope->se_u.se_try.ts_catch_label = 0; |
| 1541 | } |
Bram Moolenaar | 53c2961 | 2022-01-12 16:18:18 +0000 | [diff] [blame] | 1542 | scope->se_u.se_try.ts_has_finally = TRUE; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1543 | if (generate_instr(cctx, ISN_FINALLY) == NULL) |
| 1544 | return NULL; |
| 1545 | } |
| 1546 | |
| 1547 | return arg; |
| 1548 | } |
| 1549 | |
| 1550 | char_u * |
| 1551 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 1552 | { |
| 1553 | scope_T *scope = cctx->ctx_scope; |
| 1554 | garray_T *instr = &cctx->ctx_instr; |
| 1555 | isn_T *try_isn; |
| 1556 | |
| 1557 | if (misplaced_cmdmod(cctx)) |
| 1558 | return NULL; |
| 1559 | |
| 1560 | // end block scope from :catch or :finally |
| 1561 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 1562 | compile_endblock(cctx); |
| 1563 | scope = cctx->ctx_scope; |
| 1564 | |
| 1565 | // Error if not in a :try scope |
| 1566 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 1567 | { |
| 1568 | if (scope == NULL) |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1569 | emsg(_(e_endtry_without_try)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1570 | else if (scope->se_type == WHILE_SCOPE) |
Bram Moolenaar | 1a99222 | 2021-12-31 17:25:48 +0000 | [diff] [blame] | 1571 | emsg(_(e_missing_endwhile)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1572 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 1a99222 | 2021-12-31 17:25:48 +0000 | [diff] [blame] | 1573 | emsg(_(e_missing_endfor)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1574 | else |
Bram Moolenaar | 1a99222 | 2021-12-31 17:25:48 +0000 | [diff] [blame] | 1575 | emsg(_(e_missing_endif)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1576 | return NULL; |
| 1577 | } |
| 1578 | |
| 1579 | try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
| 1580 | if (cctx->ctx_skip != SKIP_YES) |
| 1581 | { |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 1582 | if (try_isn->isn_arg.tryref.try_ref->try_catch == 0 |
| 1583 | && try_isn->isn_arg.tryref.try_ref->try_finally == 0) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1584 | { |
| 1585 | emsg(_(e_missing_catch_or_finally)); |
| 1586 | return NULL; |
| 1587 | } |
| 1588 | |
| 1589 | #ifdef FEAT_PROFILE |
| 1590 | if (cctx->ctx_compile_type == CT_PROFILE |
| 1591 | && ((isn_T *)instr->ga_data)[instr->ga_len - 1] |
| 1592 | .isn_type == ISN_PROF_START) |
| 1593 | // move the profile start after "endtry" so that it's not counted when |
| 1594 | // the exception is rethrown. |
| 1595 | --instr->ga_len; |
| 1596 | #endif |
| 1597 | |
| 1598 | // Fill in the "end" label in jumps at the end of the blocks, if not |
| 1599 | // done by ":finally". |
| 1600 | compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, |
| 1601 | instr->ga_len, cctx); |
| 1602 | |
| 1603 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 1604 | { |
| 1605 | // Last catch without match jumps here |
| 1606 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 1607 | + scope->se_u.se_try.ts_catch_label; |
| 1608 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 1609 | } |
| 1610 | } |
| 1611 | |
Bram Moolenaar | 53c2961 | 2022-01-12 16:18:18 +0000 | [diff] [blame] | 1612 | // If there is a finally clause that ends in return then we will return. |
| 1613 | // If one of the blocks didn't end in "return" or we did not catch all |
| 1614 | // exceptions reset the had_return flag. |
| 1615 | if (!(scope->se_u.se_try.ts_has_finally && cctx->ctx_had_return) |
| 1616 | && (scope->se_u.se_try.ts_no_return |
| 1617 | || !scope->se_u.se_try.ts_caught_all)) |
| 1618 | cctx->ctx_had_return = FALSE; |
| 1619 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1620 | compile_endblock(cctx); |
| 1621 | |
| 1622 | if (cctx->ctx_skip != SKIP_YES) |
| 1623 | { |
| 1624 | // End :catch or :finally scope: set instruction index in ISN_TRY |
| 1625 | // instruction |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 1626 | try_isn->isn_arg.tryref.try_ref->try_endtry = instr->ga_len; |
Bram Moolenaar | fe15499 | 2022-03-22 20:42:12 +0000 | [diff] [blame] | 1627 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1628 | return NULL; |
| 1629 | #ifdef FEAT_PROFILE |
| 1630 | if (cctx->ctx_compile_type == CT_PROFILE) |
| 1631 | generate_instr(cctx, ISN_PROF_START); |
| 1632 | #endif |
| 1633 | } |
| 1634 | return arg; |
| 1635 | } |
| 1636 | |
| 1637 | /* |
| 1638 | * compile "throw {expr}" |
| 1639 | */ |
| 1640 | char_u * |
| 1641 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 1642 | { |
| 1643 | char_u *p = skipwhite(arg); |
| 1644 | |
| 1645 | if (compile_expr0(&p, cctx) == FAIL) |
| 1646 | return NULL; |
| 1647 | if (cctx->ctx_skip == SKIP_YES) |
| 1648 | return p; |
| 1649 | if (may_generate_2STRING(-1, FALSE, cctx) == FAIL) |
| 1650 | return NULL; |
| 1651 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 1652 | return NULL; |
| 1653 | |
| 1654 | return p; |
| 1655 | } |
| 1656 | |
| 1657 | char_u * |
| 1658 | compile_eval(char_u *arg, cctx_T *cctx) |
| 1659 | { |
| 1660 | char_u *p = arg; |
| 1661 | int name_only; |
| 1662 | long lnum = SOURCING_LNUM; |
| 1663 | |
| 1664 | // find_ex_command() will consider a variable name an expression, assuming |
| 1665 | // that something follows on the next line. Check that something actually |
| 1666 | // follows, otherwise it's probably a misplaced command. |
| 1667 | name_only = cmd_is_name_only(arg); |
| 1668 | |
| 1669 | if (compile_expr0(&p, cctx) == FAIL) |
| 1670 | return NULL; |
| 1671 | |
| 1672 | if (name_only && lnum == SOURCING_LNUM) |
| 1673 | { |
| 1674 | semsg(_(e_expression_without_effect_str), arg); |
| 1675 | return NULL; |
| 1676 | } |
| 1677 | |
| 1678 | // drop the result |
| 1679 | generate_instr_drop(cctx, ISN_DROP, 1); |
| 1680 | |
| 1681 | return skipwhite(p); |
| 1682 | } |
| 1683 | |
| 1684 | /* |
| 1685 | * compile "echo expr" |
| 1686 | * compile "echomsg expr" |
| 1687 | * compile "echoerr expr" |
| 1688 | * compile "echoconsole expr" |
| 1689 | * compile "execute expr" |
| 1690 | */ |
| 1691 | char_u * |
| 1692 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
| 1693 | { |
| 1694 | char_u *p = arg; |
| 1695 | char_u *prev = arg; |
| 1696 | char_u *expr_start; |
| 1697 | int count = 0; |
| 1698 | int start_ctx_lnum = cctx->ctx_lnum; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1699 | type_T *type; |
| 1700 | |
| 1701 | for (;;) |
| 1702 | { |
| 1703 | if (ends_excmd2(prev, p)) |
| 1704 | break; |
| 1705 | expr_start = p; |
| 1706 | if (compile_expr0(&p, cctx) == FAIL) |
| 1707 | return NULL; |
| 1708 | |
| 1709 | if (cctx->ctx_skip != SKIP_YES) |
| 1710 | { |
| 1711 | // check for non-void type |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1712 | type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1713 | if (type->tt_type == VAR_VOID) |
| 1714 | { |
| 1715 | semsg(_(e_expression_does_not_result_in_value_str), expr_start); |
| 1716 | return NULL; |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | ++count; |
| 1721 | prev = p; |
| 1722 | p = skipwhite(p); |
| 1723 | } |
| 1724 | |
| 1725 | if (count > 0) |
| 1726 | { |
| 1727 | long save_lnum = cctx->ctx_lnum; |
| 1728 | |
| 1729 | // Use the line number where the command started. |
| 1730 | cctx->ctx_lnum = start_ctx_lnum; |
| 1731 | |
| 1732 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 1733 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 1734 | else if (cmdidx == CMD_execute) |
| 1735 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 1736 | else if (cmdidx == CMD_echomsg) |
| 1737 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 1738 | else if (cmdidx == CMD_echoconsole) |
| 1739 | generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count); |
| 1740 | else |
| 1741 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
| 1742 | |
| 1743 | cctx->ctx_lnum = save_lnum; |
| 1744 | } |
| 1745 | return p; |
| 1746 | } |
| 1747 | |
| 1748 | /* |
| 1749 | * If "eap" has a range that is not a constant generate an ISN_RANGE |
| 1750 | * instruction to compute it and return OK. |
| 1751 | * Otherwise return FAIL, the caller must deal with any range. |
| 1752 | */ |
| 1753 | static int |
| 1754 | compile_variable_range(exarg_T *eap, cctx_T *cctx) |
| 1755 | { |
| 1756 | char_u *range_end = skip_range(eap->cmd, TRUE, NULL); |
| 1757 | char_u *p = skipdigits(eap->cmd); |
| 1758 | |
| 1759 | if (p == range_end) |
| 1760 | return FAIL; |
| 1761 | return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd)); |
| 1762 | } |
| 1763 | |
| 1764 | /* |
| 1765 | * :put r |
| 1766 | * :put ={expr} |
| 1767 | */ |
| 1768 | char_u * |
| 1769 | compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 1770 | { |
| 1771 | char_u *line = arg; |
| 1772 | linenr_T lnum; |
| 1773 | char *errormsg; |
| 1774 | int above = eap->forceit; |
| 1775 | |
| 1776 | eap->regname = *line; |
| 1777 | |
| 1778 | if (eap->regname == '=') |
| 1779 | { |
Bram Moolenaar | d0b7bfa | 2022-03-12 14:51:16 +0000 | [diff] [blame] | 1780 | char_u *p = skipwhite(line + 1); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1781 | |
| 1782 | if (compile_expr0(&p, cctx) == FAIL) |
| 1783 | return NULL; |
| 1784 | line = p; |
| 1785 | } |
| 1786 | else if (eap->regname != NUL) |
| 1787 | ++line; |
| 1788 | |
| 1789 | if (compile_variable_range(eap, cctx) == OK) |
| 1790 | { |
| 1791 | lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE; |
| 1792 | } |
| 1793 | else |
| 1794 | { |
| 1795 | // Either no range or a number. |
| 1796 | // "errormsg" will not be set because the range is ADDR_LINES. |
| 1797 | if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL) |
| 1798 | // cannot happen |
| 1799 | return NULL; |
| 1800 | if (eap->addr_count == 0) |
| 1801 | lnum = -1; |
| 1802 | else |
| 1803 | lnum = eap->line2; |
| 1804 | if (above) |
| 1805 | --lnum; |
| 1806 | } |
| 1807 | |
| 1808 | generate_PUT(cctx, eap->regname, lnum); |
| 1809 | return line; |
| 1810 | } |
| 1811 | |
| 1812 | /* |
| 1813 | * A command that is not compiled, execute with legacy code. |
| 1814 | */ |
| 1815 | char_u * |
| 1816 | compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx) |
| 1817 | { |
| 1818 | char_u *line = line_arg; |
| 1819 | char_u *p; |
| 1820 | int has_expr = FALSE; |
| 1821 | char_u *nextcmd = (char_u *)""; |
| 1822 | char_u *tofree = NULL; |
| 1823 | char_u *cmd_arg = NULL; |
| 1824 | |
| 1825 | if (cctx->ctx_skip == SKIP_YES) |
| 1826 | goto theend; |
| 1827 | |
| 1828 | // If there was a prececing command modifier, drop it and include it in the |
| 1829 | // EXEC command. |
| 1830 | if (cctx->ctx_has_cmdmod) |
| 1831 | { |
| 1832 | garray_T *instr = &cctx->ctx_instr; |
| 1833 | isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 1834 | |
| 1835 | if (isn->isn_type == ISN_CMDMOD) |
| 1836 | { |
| 1837 | vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod |
| 1838 | ->cmod_filter_regmatch.regprog); |
| 1839 | vim_free(isn->isn_arg.cmdmod.cf_cmdmod); |
| 1840 | --instr->ga_len; |
| 1841 | cctx->ctx_has_cmdmod = FALSE; |
| 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
| 1846 | { |
| 1847 | long argt = eap->argt; |
| 1848 | int usefilter = FALSE; |
| 1849 | |
| 1850 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 1851 | |
| 1852 | // If the command can be followed by a bar, find the bar and truncate |
| 1853 | // it, so that the following command can be compiled. |
| 1854 | // The '|' is overwritten with a NUL, it is put back below. |
| 1855 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 1856 | && *eap->arg == '!') |
| 1857 | // :w !filter or :r !filter or :r! filter |
| 1858 | usefilter = TRUE; |
| 1859 | if ((argt & EX_TRLBAR) && !usefilter) |
| 1860 | { |
| 1861 | eap->argt = argt; |
Bram Moolenaar | ac48506 | 2022-03-23 19:45:01 +0000 | [diff] [blame] | 1862 | separate_nextcmd(eap, TRUE); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1863 | if (eap->nextcmd != NULL) |
| 1864 | nextcmd = eap->nextcmd; |
| 1865 | } |
| 1866 | else if (eap->cmdidx == CMD_wincmd) |
| 1867 | { |
| 1868 | p = eap->arg; |
| 1869 | if (*p != NUL) |
| 1870 | ++p; |
| 1871 | if (*p == 'g' || *p == Ctrl_G) |
| 1872 | ++p; |
| 1873 | p = skipwhite(p); |
| 1874 | if (*p == '|') |
| 1875 | { |
| 1876 | *p = NUL; |
| 1877 | nextcmd = p + 1; |
| 1878 | } |
| 1879 | } |
| 1880 | else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd) |
| 1881 | { |
| 1882 | // If there is a trailing '{' read lines until the '}' |
| 1883 | p = eap->arg + STRLEN(eap->arg) - 1; |
| 1884 | while (p > eap->arg && VIM_ISWHITE(*p)) |
| 1885 | --p; |
| 1886 | if (*p == '{') |
| 1887 | { |
| 1888 | exarg_T ea; |
Bram Moolenaar | 62628d9 | 2022-02-25 21:10:53 +0000 | [diff] [blame] | 1889 | int flags = 0; // unused |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1890 | int start_lnum = SOURCING_LNUM; |
| 1891 | |
| 1892 | CLEAR_FIELD(ea); |
| 1893 | ea.arg = eap->arg; |
| 1894 | fill_exarg_from_cctx(&ea, cctx); |
| 1895 | (void)may_get_cmd_block(&ea, p, &tofree, &flags); |
| 1896 | if (tofree != NULL) |
| 1897 | { |
| 1898 | *p = NUL; |
| 1899 | line = concat_str(line, tofree); |
| 1900 | if (line == NULL) |
| 1901 | goto theend; |
| 1902 | vim_free(tofree); |
| 1903 | tofree = line; |
| 1904 | SOURCING_LNUM = start_lnum; |
| 1905 | } |
| 1906 | } |
| 1907 | } |
| 1908 | } |
| 1909 | |
| 1910 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 1911 | { |
| 1912 | // expand filename in "syntax include [@group] filename" |
| 1913 | has_expr = TRUE; |
| 1914 | eap->arg = skipwhite(eap->arg + 7); |
| 1915 | if (*eap->arg == '@') |
| 1916 | eap->arg = skiptowhite(eap->arg); |
| 1917 | } |
| 1918 | |
| 1919 | if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal) |
| 1920 | && STRLEN(eap->arg) > 4) |
| 1921 | { |
| 1922 | int delim = *eap->arg; |
| 1923 | |
| 1924 | p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL); |
| 1925 | if (*p == delim) |
| 1926 | cmd_arg = p + 1; |
| 1927 | } |
| 1928 | |
| 1929 | if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed) |
| 1930 | cmd_arg = eap->arg; |
| 1931 | |
| 1932 | if (cmd_arg != NULL) |
| 1933 | { |
| 1934 | exarg_T nea; |
| 1935 | |
| 1936 | CLEAR_FIELD(nea); |
| 1937 | nea.cmd = cmd_arg; |
| 1938 | p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL); |
| 1939 | if (nea.cmdidx < CMD_SIZE) |
| 1940 | { |
| 1941 | has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND); |
| 1942 | if (has_expr) |
| 1943 | eap->arg = skiptowhite(eap->arg); |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
| 1948 | { |
| 1949 | int count = 0; |
| 1950 | char_u *start = skipwhite(line); |
| 1951 | |
| 1952 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 1953 | // PUSHS ":cmd xxx" |
| 1954 | // eval expr1 |
| 1955 | // PUSHS "yyy" |
| 1956 | // eval expr2 |
| 1957 | // PUSHS "zzz" |
| 1958 | // EXECCONCAT 5 |
| 1959 | for (;;) |
| 1960 | { |
| 1961 | if (p > start) |
| 1962 | { |
| 1963 | char_u *val = vim_strnsave(start, p - start); |
| 1964 | |
| 1965 | generate_PUSHS(cctx, &val); |
| 1966 | ++count; |
| 1967 | } |
| 1968 | p += 2; |
| 1969 | if (compile_expr0(&p, cctx) == FAIL) |
| 1970 | return NULL; |
| 1971 | may_generate_2STRING(-1, TRUE, cctx); |
| 1972 | ++count; |
| 1973 | p = skipwhite(p); |
| 1974 | if (*p != '`') |
| 1975 | { |
| 1976 | emsg(_(e_missing_backtick)); |
| 1977 | return NULL; |
| 1978 | } |
| 1979 | start = p + 1; |
| 1980 | |
| 1981 | p = (char_u *)strstr((char *)start, "`="); |
| 1982 | if (p == NULL) |
| 1983 | { |
| 1984 | if (*skipwhite(start) != NUL) |
| 1985 | { |
| 1986 | char_u *val = vim_strsave(start); |
| 1987 | |
| 1988 | generate_PUSHS(cctx, &val); |
| 1989 | ++count; |
| 1990 | } |
| 1991 | break; |
| 1992 | } |
| 1993 | } |
| 1994 | generate_EXECCONCAT(cctx, count); |
| 1995 | } |
| 1996 | else |
| 1997 | generate_EXEC_copy(cctx, ISN_EXEC, line); |
| 1998 | |
| 1999 | theend: |
| 2000 | if (*nextcmd != NUL) |
| 2001 | { |
| 2002 | // the parser expects a pointer to the bar, put it back |
| 2003 | --nextcmd; |
| 2004 | *nextcmd = '|'; |
| 2005 | } |
| 2006 | vim_free(tofree); |
| 2007 | |
| 2008 | return nextcmd; |
| 2009 | } |
| 2010 | |
| 2011 | /* |
| 2012 | * A script command with heredoc, e.g. |
| 2013 | * ruby << EOF |
| 2014 | * command |
| 2015 | * EOF |
| 2016 | * Has been turned into one long line with NL characters by |
| 2017 | * get_function_body(): |
| 2018 | * ruby << EOF<NL> command<NL>EOF |
| 2019 | */ |
| 2020 | char_u * |
| 2021 | compile_script(char_u *line, cctx_T *cctx) |
| 2022 | { |
| 2023 | if (cctx->ctx_skip != SKIP_YES) |
| 2024 | { |
| 2025 | isn_T *isn; |
| 2026 | |
| 2027 | if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL) |
| 2028 | return NULL; |
| 2029 | isn->isn_arg.string = vim_strsave(line); |
| 2030 | } |
| 2031 | return (char_u *)""; |
| 2032 | } |
| 2033 | |
| 2034 | |
| 2035 | /* |
| 2036 | * :s/pat/repl/ |
| 2037 | */ |
| 2038 | char_u * |
| 2039 | compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 2040 | { |
| 2041 | char_u *cmd = eap->arg; |
| 2042 | char_u *expr = (char_u *)strstr((char *)cmd, "\\="); |
| 2043 | |
| 2044 | if (expr != NULL) |
| 2045 | { |
| 2046 | int delimiter = *cmd++; |
| 2047 | |
| 2048 | // There is a \=expr, find it in the substitute part. |
| 2049 | cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL); |
| 2050 | if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=') |
| 2051 | { |
| 2052 | garray_T save_ga = cctx->ctx_instr; |
| 2053 | char_u *end; |
| 2054 | int expr_res; |
| 2055 | int trailing_error; |
| 2056 | int instr_count; |
| 2057 | isn_T *instr; |
| 2058 | isn_T *isn; |
| 2059 | |
| 2060 | cmd += 3; |
| 2061 | end = skip_substitute(cmd, delimiter); |
| 2062 | |
| 2063 | // Temporarily reset the list of instructions so that the jump |
| 2064 | // labels are correct. |
| 2065 | cctx->ctx_instr.ga_len = 0; |
| 2066 | cctx->ctx_instr.ga_maxlen = 0; |
| 2067 | cctx->ctx_instr.ga_data = NULL; |
| 2068 | expr_res = compile_expr0(&cmd, cctx); |
| 2069 | if (end[-1] == NUL) |
| 2070 | end[-1] = delimiter; |
| 2071 | cmd = skipwhite(cmd); |
| 2072 | trailing_error = *cmd != delimiter && *cmd != NUL; |
| 2073 | |
| 2074 | if (expr_res == FAIL || trailing_error |
| 2075 | || GA_GROW_FAILS(&cctx->ctx_instr, 1)) |
| 2076 | { |
| 2077 | if (trailing_error) |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 2078 | semsg(_(e_trailing_characters_str), cmd); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2079 | clear_instr_ga(&cctx->ctx_instr); |
| 2080 | cctx->ctx_instr = save_ga; |
| 2081 | return NULL; |
| 2082 | } |
| 2083 | |
| 2084 | // Move the generated instructions into the ISN_SUBSTITUTE |
| 2085 | // instructions, then restore the list of instructions before |
| 2086 | // adding the ISN_SUBSTITUTE instruction. |
| 2087 | instr_count = cctx->ctx_instr.ga_len; |
| 2088 | instr = cctx->ctx_instr.ga_data; |
| 2089 | instr[instr_count].isn_type = ISN_FINISH; |
| 2090 | |
| 2091 | cctx->ctx_instr = save_ga; |
| 2092 | if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL) |
| 2093 | { |
| 2094 | int idx; |
| 2095 | |
| 2096 | for (idx = 0; idx < instr_count; ++idx) |
| 2097 | delete_instr(instr + idx); |
| 2098 | vim_free(instr); |
| 2099 | return NULL; |
| 2100 | } |
| 2101 | isn->isn_arg.subs.subs_cmd = vim_strsave(arg); |
| 2102 | isn->isn_arg.subs.subs_instr = instr; |
| 2103 | |
| 2104 | // skip over flags |
| 2105 | if (*end == '&') |
| 2106 | ++end; |
| 2107 | while (ASCII_ISALPHA(*end) || *end == '#') |
| 2108 | ++end; |
| 2109 | return end; |
| 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | return compile_exec(arg, eap, cctx); |
| 2114 | } |
| 2115 | |
| 2116 | char_u * |
| 2117 | compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 2118 | { |
| 2119 | char_u *arg = eap->arg; |
| 2120 | lhs_T *lhs = &cctx->ctx_redir_lhs; |
| 2121 | |
| 2122 | if (lhs->lhs_name != NULL) |
| 2123 | { |
| 2124 | if (STRNCMP(arg, "END", 3) == 0) |
| 2125 | { |
| 2126 | if (lhs->lhs_append) |
| 2127 | { |
| 2128 | // First load the current variable value. |
| 2129 | if (compile_load_lhs_with_index(lhs, lhs->lhs_whole, |
| 2130 | cctx) == FAIL) |
| 2131 | return NULL; |
| 2132 | } |
| 2133 | |
| 2134 | // Gets the redirected text and put it on the stack, then store it |
| 2135 | // in the variable. |
| 2136 | generate_instr_type(cctx, ISN_REDIREND, &t_string); |
| 2137 | |
| 2138 | if (lhs->lhs_append) |
LemonBoy | 372bcce | 2022-04-25 12:43:20 +0100 | [diff] [blame] | 2139 | generate_CONCAT(cctx, 2); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2140 | |
| 2141 | if (lhs->lhs_has_index) |
| 2142 | { |
| 2143 | // Use the info in "lhs" to store the value at the index in the |
| 2144 | // list or dict. |
| 2145 | if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE, |
| 2146 | &t_string, cctx) == FAIL) |
| 2147 | return NULL; |
| 2148 | } |
Bram Moolenaar | 5cd6479 | 2021-12-25 18:23:24 +0000 | [diff] [blame] | 2149 | else if (generate_store_lhs(cctx, lhs, -1, FALSE) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2150 | return NULL; |
| 2151 | |
| 2152 | VIM_CLEAR(lhs->lhs_name); |
| 2153 | VIM_CLEAR(lhs->lhs_whole); |
| 2154 | return arg + 3; |
| 2155 | } |
| 2156 | emsg(_(e_cannot_nest_redir)); |
| 2157 | return NULL; |
| 2158 | } |
| 2159 | |
| 2160 | if (arg[0] == '=' && arg[1] == '>') |
| 2161 | { |
| 2162 | int append = FALSE; |
| 2163 | |
| 2164 | // redirect to a variable is compiled |
| 2165 | arg += 2; |
| 2166 | if (*arg == '>') |
| 2167 | { |
| 2168 | ++arg; |
| 2169 | append = TRUE; |
| 2170 | } |
| 2171 | arg = skipwhite(arg); |
| 2172 | |
| 2173 | if (compile_assign_lhs(arg, lhs, CMD_redir, |
Bram Moolenaar | 97f8c10 | 2022-04-02 19:43:57 +0100 | [diff] [blame] | 2174 | FALSE, FALSE, FALSE, 1, cctx) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2175 | return NULL; |
| 2176 | if (need_type(&t_string, lhs->lhs_member_type, |
| 2177 | -1, 0, cctx, FALSE, FALSE) == FAIL) |
| 2178 | return NULL; |
| 2179 | generate_instr(cctx, ISN_REDIRSTART); |
| 2180 | lhs->lhs_append = append; |
| 2181 | if (lhs->lhs_has_index) |
| 2182 | { |
| 2183 | lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total); |
| 2184 | if (lhs->lhs_whole == NULL) |
| 2185 | return NULL; |
| 2186 | } |
| 2187 | |
| 2188 | return arg + lhs->lhs_varlen_total; |
| 2189 | } |
| 2190 | |
| 2191 | // other redirects are handled like at script level |
| 2192 | return compile_exec(line, eap, cctx); |
| 2193 | } |
| 2194 | |
| 2195 | #if defined(FEAT_QUICKFIX) || defined(PROTO) |
| 2196 | char_u * |
| 2197 | compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 2198 | { |
| 2199 | isn_T *isn; |
| 2200 | char_u *p; |
| 2201 | |
| 2202 | isn = generate_instr(cctx, ISN_CEXPR_AUCMD); |
| 2203 | if (isn == NULL) |
| 2204 | return NULL; |
| 2205 | isn->isn_arg.number = eap->cmdidx; |
| 2206 | |
| 2207 | p = eap->arg; |
| 2208 | if (compile_expr0(&p, cctx) == FAIL) |
| 2209 | return NULL; |
| 2210 | |
| 2211 | isn = generate_instr(cctx, ISN_CEXPR_CORE); |
| 2212 | if (isn == NULL) |
| 2213 | return NULL; |
| 2214 | isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T); |
| 2215 | if (isn->isn_arg.cexpr.cexpr_ref == NULL) |
| 2216 | return NULL; |
| 2217 | isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx; |
| 2218 | isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit; |
| 2219 | isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line)); |
| 2220 | |
| 2221 | return p; |
| 2222 | } |
| 2223 | #endif |
| 2224 | |
| 2225 | /* |
| 2226 | * Compile "return [expr]". |
| 2227 | * When "legacy" is TRUE evaluate [expr] with legacy syntax |
| 2228 | */ |
| 2229 | char_u * |
| 2230 | compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx) |
| 2231 | { |
| 2232 | char_u *p = arg; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2233 | type_T *stack_type; |
| 2234 | |
| 2235 | if (*p != NUL && *p != '|' && *p != '\n') |
| 2236 | { |
Bram Moolenaar | 0bfa849 | 2022-01-22 12:27:04 +0000 | [diff] [blame] | 2237 | // For a lambda, "return expr" is always used, also when "expr" results |
| 2238 | // in a void. |
| 2239 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 2240 | && (cctx->ctx_ufunc->uf_flags & FC_LAMBDA) == 0) |
Bram Moolenaar | ef7aadb | 2022-01-18 18:46:07 +0000 | [diff] [blame] | 2241 | { |
| 2242 | emsg(_(e_returning_value_in_function_without_return_type)); |
| 2243 | return NULL; |
| 2244 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2245 | if (legacy) |
| 2246 | { |
| 2247 | int save_flags = cmdmod.cmod_flags; |
| 2248 | |
| 2249 | generate_LEGACY_EVAL(cctx, p); |
| 2250 | if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1, |
| 2251 | 0, cctx, FALSE, FALSE) == FAIL) |
| 2252 | return NULL; |
| 2253 | cmdmod.cmod_flags |= CMOD_LEGACY; |
| 2254 | (void)skip_expr(&p, NULL); |
| 2255 | cmdmod.cmod_flags = save_flags; |
| 2256 | } |
| 2257 | else |
| 2258 | { |
| 2259 | // compile return argument into instructions |
| 2260 | if (compile_expr0(&p, cctx) == FAIL) |
| 2261 | return NULL; |
| 2262 | } |
| 2263 | |
| 2264 | if (cctx->ctx_skip != SKIP_YES) |
| 2265 | { |
| 2266 | // "check_return_type" with uf_ret_type set to &t_unknown is used |
| 2267 | // for an inline function without a specified return type. Set the |
| 2268 | // return type here. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 2269 | stack_type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2270 | if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL |
Bram Moolenaar | 1a572e9 | 2022-03-15 12:28:10 +0000 | [diff] [blame] | 2271 | || cctx->ctx_ufunc->uf_ret_type == &t_unknown)) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2272 | || (!check_return_type |
| 2273 | && cctx->ctx_ufunc->uf_ret_type == &t_unknown)) |
| 2274 | { |
| 2275 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
| 2276 | } |
| 2277 | else |
| 2278 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2279 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 2280 | 0, cctx, FALSE, FALSE) == FAIL) |
| 2281 | return NULL; |
| 2282 | } |
| 2283 | } |
| 2284 | } |
| 2285 | else |
| 2286 | { |
| 2287 | // "check_return_type" cannot be TRUE, only used for a lambda which |
| 2288 | // always has an argument. |
| 2289 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 2290 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
| 2291 | { |
| 2292 | emsg(_(e_missing_return_value)); |
| 2293 | return NULL; |
| 2294 | } |
| 2295 | |
| 2296 | // No argument, return zero. |
| 2297 | generate_PUSHNR(cctx, 0); |
| 2298 | } |
| 2299 | |
| 2300 | // Undo any command modifiers. |
| 2301 | generate_undo_cmdmods(cctx); |
| 2302 | |
| 2303 | if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL) |
| 2304 | return NULL; |
| 2305 | |
| 2306 | // "return val | endif" is possible |
| 2307 | return skipwhite(p); |
| 2308 | } |
| 2309 | |
| 2310 | /* |
| 2311 | * Check if the separator for a :global or :substitute command is OK. |
| 2312 | */ |
| 2313 | int |
| 2314 | check_global_and_subst(char_u *cmd, char_u *arg) |
| 2315 | { |
| 2316 | if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL) |
| 2317 | { |
| 2318 | semsg(_(e_separator_not_supported_str), arg); |
| 2319 | return FAIL; |
| 2320 | } |
| 2321 | if (VIM_ISWHITE(cmd[1])) |
| 2322 | { |
| 2323 | semsg(_(e_no_white_space_allowed_before_separator_str), cmd); |
| 2324 | return FAIL; |
| 2325 | } |
| 2326 | return OK; |
| 2327 | } |
| 2328 | |
| 2329 | |
| 2330 | #endif // defined(FEAT_EVAL) |